Reputation: 245
In my app, all of the stuff is only in Landscape mode. I don't want the app to be functional in Portrait mode. How do I limit the orientation?
Thanks.
Upvotes: 7
Views: 2171
Reputation: 96
For people looking to answer this question who aren't writing a 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 let the Orientation change, however if you are interested in only allowing Landscape you 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 you Main Window.xaml:
<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>
So we really aren't limiting the Orientation, we are just noticing when it happens, and re rotating our UI so it still looks like it is in Portrait mode :) Again this is mostly for non Metro Win 8 applications and or applications that also run on Win 7 tablets.
Upvotes: 0
Reputation: 185
I had this problem as well as I wanted to constrain my game to only landscape mode. I put this in my OnLaunched handler for App.xaml:
Windows.Graphics.Display.DisplayProperties.AutoRotationPreferences =
Windows.Graphics.Display.DisplayOrientations.Landscape;
However I noted that in the simulator it seemed to ignore this whereas on the hardware tablet I tested on it seemed to behave appropriately. The AutoRotationPreferences are bit flags so you can or together all the orientations you want to allow.
Upvotes: 4
Reputation: 6450
As explained in this link the orientation limitation preference setting of the app is only enforced on a Windows 8 system with a supported HARDWARE ACCELEROMETER. This means that unless Windows knows how the system is orientated through the means of a supported sensor, it will not attempt to switch to the app's preferred orientation.
So it will all depend on the user's hardware.
Upvotes: 4