2

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?

ashchuk
  • 331
  • 5
  • 19

3 Answers3

4

Maked like lokusking said.

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

SetAutoRotation(false);

Here is a link.

Community
  • 1
  • 1
ashchuk
  • 331
  • 5
  • 19
  • 1
    Per a comment on that link: "This function is not documented or supported anywhere. It won't pass store certification or even Win32 certification of any kind. Please do not use it (ever). MSFT reserves the right to remove it at any time" Feels like an unsafe hack. – Geoffrey Hudik Nov 10 '17 at 18:21
1

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
        }));
    }
}
A.J.Bauer
  • 2,803
  • 1
  • 26
  • 35
  • It's doesn't work. Placed breakpoint into `Application_Current_Resuming`, it's just has not been invoked. – ashchuk Jun 22 '16 at 13:49
  • Did you try the Display orientation sample? – A.J.Bauer Jun 22 '16 at 13:56
  • Yep, i tried. And I tried to add method into `Application.Current.Resuming`. But Resuming method doesn't invoked when I push Start. – ashchuk Jun 22 '16 at 14:02
  • You should really read the link App lifecycle.. It also depends on what page you suspended.. this is just a sample for MainPage. You can also place this.Resuming += App_Resuming; in your App() constructor in App.xaml.cs but then if you are working with multiple views you have some more things to do.. – A.J.Bauer Jun 22 '16 at 14:06
  • You first suspend and then resume (Lifecycle events at the top of visual studio after running).. – A.J.Bauer Jun 22 '16 at 14:07
  • Oh, I tried to place it in `App()` too. This thing working when I suspend app this way: push "Back", then make right swipe and open suspended app again. This way orientation is saving. But this doesn't works for Start: push "Start" (app is suspended), then push "Start" again (app resumed). This way makes orientation preferences reset. I tried it it this "Display orientation sample", here is just one view and I tried this trick for this single view. – ashchuk Jun 22 '16 at 14:16
  • Mmm I'd place a breakpoint in OnLaunched in App.xaml.cs also to see if it gets there in this case (if so check LaunchActivatedEventArgs). Then it should also hit OnNavigatedTo of the current page. – A.J.Bauer Jun 22 '16 at 14:21
  • I'm sure you can get this to work somehow but you might want to consider doing a page layout that works both ways and looks good for different screen sizes.. – A.J.Bauer Jun 22 '16 at 14:29
1

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

NGame
  • 82
  • 1
  • 6