For people who want to answer this question who donβt write the Metro app (where you can set preferred orientations in the manifest or have access to Windows.Graphics.Display.DisplayProperties.AutoRotationPreferences ) ...
There is no real way to NOT allow a change in Orientation, however, if you are only interested in the fact that the Landscape could do something like this:
View Model:
Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged); } public bool IsLandscape { get; set; } void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) { if (SystemParameters.PrimaryScreenWidth > SystemParameters.PrimaryScreenHeight) { IsLandscape = true; } else { IsLandscape = false; } RaisePropertyChanged( "IsLandscape" ); }
In the main .xaml window:
<Border > <Border.Style> <Style TargetType="{x:Type Border}"> <Style.Triggers> <DataTrigger Binding="{Binding IsLandscape}" Value="False"> <Setter Property="LayoutTransform"> <Setter.Value> <RotateTransform Angle="90"/> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style> </Border.Style> ///The rest of your controls and UI </Border>
Thus, we really do not limit Orientation, we just notice when this happens, and rotate our user interface so that it still looks like in portrait mode :) Again, this is mainly for applications that do not support Metro Win 8, and applications that also run on Win 7 tablets.
Brock
source share