Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable only landscape mode in a UWP app?

Tags:

c#

uwp

I have already specified in the manifest as Landscape and LandscapeFlipped, I know this is just a preference and on top of this I've added the below code in the App.xaml.cs OnLaunched. But yet when tested on a tablet the app goes back to portrait mode.

 DisplayInformation.AutoRotationPreferences = 
                     DisplayOrientations.Landscape | DisplayOrientations.LandscapeFlipped;

Also is there any alternative way I could test this in a simulator instead of an actual tablet/device?

Thanks in advance.

like image 944
AbsoluteSith Avatar asked Jul 27 '16 08:07

AbsoluteSith


Video Answer


2 Answers

Since my comment helped you im going to add it as answer

Reference

Solution

[DllImport("user32.dll", EntryPoint = "#2507")]
extern static bool SetAutoRotation(bool bEnable);

SetAutoRotation(false);
like image 65
lokusking Avatar answered Oct 09 '22 18:10

lokusking


First a note: the DisplayInformation.AutoRotationPreferences provides roughly the same functionality as the Win32 SetDisplayAutoRotationPreferences function, except that the DisplayInformation one is for UWPs and the Win32 one is for Win32 apps (e.g. with HWNDs). You can't use the UWP API for Win32 apps and vice versa.

The UWP API is only designed to work when the device is in Tablet Mode on Windows 10. Auto-rotation must also be turned on by the user and the device needs to have an accelerometer. In general, your app needs to be the foreground window as well for it to work.

Now that you know all the requirements of the API (which are subject to change, by the way - this is an orientation "preference" after all), is it still not working as intended? The intention is that the user is still in control of the experience.

In some cases, I've seen developers trying to use this API for custom OEM devices running a single app in kiosk mode all the time. The API was designed for normal app scenarios, not for kiosk scenarios. If you have a scenario like this, then ideally you can just describe the hardware configuration you want to drive (like a normal OEM device - e.g. you write the ACPI tables and firmware) and the OS will use the correct orientation. If you're just building a one-off device, you could consider setting the screen rotation using SetDisplayConfig from a Win32 app on startup rather than relying on auto-rotation at all.

like image 39
zhuman - MSFT Avatar answered Oct 09 '22 18:10

zhuman - MSFT