I recently needed to prevent screen rotation in a UWP application when running on a mobile device, but the application itself supports all orientations (Landscape
, Portrait
, LandscapeFlipped
, PortraitFlipped
). In order to achieve my desired result, I added a quick static method that I found here.
This goes in App.xaml.cs
:
public static bool IsMobile { get { var qualifiers = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().QualifierValues; return (qualifiers.ContainsKey("DeviceFamily") && qualifiers["DeviceFamily"] == "Mobile"); } }
Now, in my MainPageViewModel.cs
, my constructor looks like this:
public MainPageViewModel() { if (App.IsMobile) { DisplayInformation.AutoRotationPreferences = DisplayOrientation.Portrait; } }
This will force your application to stay in Portrait
mode on a mobile device, regardless of the supported orientations in your Package.appxmanifest.