ashchuk
ashchuk

Reputation: 331

How to lock tablet screen rotation in UWP app?

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?

Upvotes: 2

Views: 3096

Answers (3)

NGame
NGame

Reputation: 82

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

Upvotes: 1

ashchuk
ashchuk

Reputation: 331

Maked like lokusking said.

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

SetAutoRotation(false);

Here is a link.

Upvotes: 4

A.J.Bauer
A.J.Bauer

Reputation: 3001

enter image description hereSome 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
        }));
    }
}

Upvotes: 1

Related Questions