How to lock tablet screen rotation in UWP app?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 04:39:33

问题


I wanted my UWP app, running on windows 10, to support only Landscape orientations, but here is a one problem. First i founded this question: link

This example from GitHub works fine then I try to set orientation with checkboxes, and when i set orientation like this:

DisplayOrientations orientations = DisplayOrientations.Landscape;
DisplayInformation.AutoRotationPreferences = orientations;

it works too. Great.

But here is an issue. If you'll try to press Start to suspend app and press it again to resume app all rotation preferences will be set in default. It works like reset for rotation preferences.

I tried to set Suspending method, but it doesn't work. Tried with debugger and without it, this doesn't works. Setting "Supported rotations" and "Lock Screen" declaration in manifest file doesn't work too. Can somebody help me?


回答1:


Maked like lokusking said.

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

SetAutoRotation(false);

Here is a link.




回答2:


Some usefull links: Create Windows apps, App lifecycle, Display orientation sample.

If setting this in Package.appxmanifest (double click / Landscape) is not enough if in tablet mode then you could try to set things when the App is resuming.

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        Application.Current.Resuming += Application_Current_Resuming;
    }

    private async void Application_Current_Resuming(object sender, object e)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
        {
            // Your code here
        }));
    }
}



回答3:


The best solution is here I think: You can lock the app in Landscape mode for example:

Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = Windows.Graphics.Display.DisplayOrientations.Landscape;

Then when you want to return auto rotation to user use the following code:

Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = Windows.Graphics.Display.DisplayOrientations.Landscape | Windows.Graphics.Display.DisplayOrientations.LandscapeFlipped
                | Windows.Graphics.Display.DisplayOrientations.None | Windows.Graphics.Display.DisplayOrientations.Portrait | Windows.Graphics.Display.DisplayOrientations.PortraitFlipped;

Regards



来源:https://stackoverflow.com/questions/37968246/how-to-lock-tablet-screen-rotation-in-uwp-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!