- 1 year ago
How to set the orientation of a UWP app programmatically
In a UWP (Universal Windows Platform) app, you can programmatically set the orientation using the DisplayInformation
class. Here's an example in C#:
csharpusing Windows.Graphics.Display;
// ...
// Set the preferred orientation
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
// Reset the preferred orientation to allow auto-rotation
DisplayInformation.AutoRotationPreferences = DisplayOrientations.None;
In this example, the AutoRotationPreferences
property of the DisplayInformation
class is used to set the preferred orientation. The DisplayOrientations
enumeration provides different orientation options such as Landscape
, Portrait
, PortraitFlipped
, LandscapeFlipped
, and None
(to allow auto-rotation). You can choose the appropriate orientation option based on your requirements.
By setting the AutoRotationPreferences
to a specific orientation, the app will attempt to remain in that orientation. However, keep in mind that the actual behavior depends on the device's hardware support and user settings. Some devices may override the preferred orientation based on their own orientation sensors.
Remember to add the necessary using statement for Windows.Graphics.Display
at the top of your file.