How to enable only landscape mode in a UWP app?

前端 未结 3 1612
醉酒成梦
醉酒成梦 2020-12-11 19:17

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

相关标签:
3条回答
  • 2020-12-11 19:58

    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.

    0 讨论(0)
  • 2020-12-11 20:04

    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);
    
    0 讨论(0)
  • 2020-12-11 20:11

    You used the right API to force the orientation in UWP app, but you can refer to the official DisplayOrientations enumeration, in the remarks part:

    Applications typically use this property to translate the reading of an accelerometer or to translate the physical button events in accordance with the current screen rotation.

    It means, these work but only if the device has an accelerometer, on laptop even simulator, they have no effect.

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

    No, simulator is not suitable, you can test this on Mobile or Mobile emulator.

    0 讨论(0)
提交回复
热议问题