locked
WinRT - AutoRotationPreferences ignores "Portrait mode only"

    Question

  • Hi,

    On a Universal App, when I want to restrict a page to a specific rotation mode using:

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

    it works fine on WP 8.1 WinRT(both landscape & portrait), but DisplayOrientations.Portrait is ignored on Windows App. The page keeps changing its orientation from Landscape to Portrait. But if I set DisplayOrientations.Landscape, the page is correctly restricted to landscape mode.

    Here's a simple XAML/C# page to reproduce it:

    XAML:

    <Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Image Source="http://www.microsoft.com/global/en-us/news/publishingimages/logos/MSFT_logo_Web.jpg" Stretch="Uniform"/>
        </Grid>
    </Page>
    

    C#:

    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Input;
    
    namespace App1
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                
                // This one works fine
                //Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = Windows.Graphics.Display.DisplayOrientations.Landscape;
                // This one doesn't work
                Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = Windows.Graphics.Display.DisplayOrientations.Portrait;
            }
        }
    }
    

    Note: I know the simulator does not support orientation change so I'm using a Microsoft Surface for my orientation tests.

    Friday, August 22, 2014 12:47 PM

Answers

  • Many thanks for your time!

    I only have one Surface for my tests so, knowing this piece of code works fine on your side made me think about an issue coming from my Surface (or bad option set).

    After digging a little and trying to play with Screen Rotation Lock on my Surface following this support topic:

    http://www.microsoft.com/surface/en-gb/support/hardware-and-drivers/troubleshoot-surface-touchscreen-rotation?lc=2057#auto

    I found out the Screen Lock icon in my Surface settings was greyed out.

    The fault to => the keyboard.

    When plugged in, the keyboard makes the Surface rotation behave differently. Both Portrait & Landscape can be used but only Landscape can be set as "locked orientation" (from Application).

    Unplugging the keyboard makes it behave like any tablet, and so, both Landscape & Portait mode can be set as "locked orientation" (from Application).

    It's not that obvious for lambda user but my problem isn't solved yet.

    I know the user has to unplug its keyboard for my page to be in portrait mode only.

    I couldn't find it in the API so I'm asking:

    - is there any method or property to tell us if the device has Screen Lock set?
    So at least, I could warn the user that the page won't behave as expected due to Screen Lock option. (A better solution would be to be able to lock portrait mode on my page without taking care of Screen Lock option but it doesn't sound possible).



    • Marked as answer by ChickenMythic Tuesday, September 02, 2014 7:25 AM
    • Edited by ChickenMythic Tuesday, September 02, 2014 7:26 AM
    Friday, August 29, 2014 6:51 AM
  • Thanks for your information, the reason might be as you mentioned the keyboard plug in lock the surface rotation. I found something interesting from documentation InitialRotationPreference:

    Note that on devices that can't be rotated, an app might be shown in that device's default orientation and the app's preferred orientation will be ignored.

    For your question: is there any method or property to tell us if the device has Screen Lock set?

    As I know, there is no such API. I think you could try to get the window size programmatically to determine if the screen is in Portrait.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by ChickenMythic Tuesday, September 02, 2014 7:25 AM
    Tuesday, September 02, 2014 2:27 AM
    Moderator

All replies

  • Hi ChickenMythic,

    If you put the Preference code in OnNavigatedTo method by rewrite it, would it be better?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, August 26, 2014 7:07 AM
    Moderator
  • I already tried to set it in OnNavigatedTo method without luck.

    But based on your answer I tried to set the Preference in the Constructor then, as you said, rewrite it in OnNavigatedTo:

    public sealed partial class MainPage : Page
    {
    	public MainPage()
    	{
    		this.InitializeComponent();
    		
    		// This one works fine
    		Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = Windows.Graphics.Display.DisplayOrientations.Landscape;
    	}
    
    	protected override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
    	{
    		base.OnNavigatedTo(e);
    
    		// This one doesn't work
    		Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = Windows.Graphics.Display.DisplayOrientations.Portrait;
    	}
    }

    It still doesn't take Portrait mode into account. But if I change Portrait to Landscape in OnNavigatedTo, only Landscape mode is available.

    So, setting the orientation Preferences in OnNavigatedTo is good but the "Portrait restriction case" still doesn't work.

    Tuesday, August 26, 2014 7:26 AM
  • Need confirm one more thing, did you set the correct thing in app manifest?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 27, 2014 9:02 AM
    Moderator
  • Thanks for you help.

    I didn't change the App Manifest parameters so, NO "Supported Rotations" box was ticked.

    I suppose no box ticked has the same behavior as all boxes ticked but... I might be wrong.

    So, I tried your way and ticked both "Landscape" and "Portrait" in the App Manifest without any changes (I set orientation Preferences to "Portrait" in code behind). My page still isn't restricted to "Portrait" mode only.

    I also tried to only tick "Portrait" in the App Manifest (with and without setting orientation Preferences in code behind) but the problem still remains.

    I'm using VS2013 Pro version 12.0.30723.00 Update 3, if this can be of any help. If you need any more information just let me know.

    I'd like to dig deeper to find out what's going on behind the scene but I don't have the resources for, so thanks for your time.

    Wednesday, August 27, 2014 10:24 AM
  • Hi,

    I still cannot reproduce the issue, I borrowed a Table from Lab which is Surface pro and test your code, everything works like a charm. Even I rotate the Tablet, the "Microsoft" image and the page still keep Portrait.

    Sorry I don't know why you cannot have Portrait mode, but everything works fine on my Surface. I would recommend you to do a test on another Surface to see if the same thing happen.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 28, 2014 11:45 AM
    Moderator
  • Many thanks for your time!

    I only have one Surface for my tests so, knowing this piece of code works fine on your side made me think about an issue coming from my Surface (or bad option set).

    After digging a little and trying to play with Screen Rotation Lock on my Surface following this support topic:

    http://www.microsoft.com/surface/en-gb/support/hardware-and-drivers/troubleshoot-surface-touchscreen-rotation?lc=2057#auto

    I found out the Screen Lock icon in my Surface settings was greyed out.

    The fault to => the keyboard.

    When plugged in, the keyboard makes the Surface rotation behave differently. Both Portrait & Landscape can be used but only Landscape can be set as "locked orientation" (from Application).

    Unplugging the keyboard makes it behave like any tablet, and so, both Landscape & Portait mode can be set as "locked orientation" (from Application).

    It's not that obvious for lambda user but my problem isn't solved yet.

    I know the user has to unplug its keyboard for my page to be in portrait mode only.

    I couldn't find it in the API so I'm asking:

    - is there any method or property to tell us if the device has Screen Lock set?
    So at least, I could warn the user that the page won't behave as expected due to Screen Lock option. (A better solution would be to be able to lock portrait mode on my page without taking care of Screen Lock option but it doesn't sound possible).



    • Marked as answer by ChickenMythic Tuesday, September 02, 2014 7:25 AM
    • Edited by ChickenMythic Tuesday, September 02, 2014 7:26 AM
    Friday, August 29, 2014 6:51 AM
  • Thanks for your information, the reason might be as you mentioned the keyboard plug in lock the surface rotation. I found something interesting from documentation InitialRotationPreference:

    Note that on devices that can't be rotated, an app might be shown in that device's default orientation and the app's preferred orientation will be ignored.

    For your question: is there any method or property to tell us if the device has Screen Lock set?

    As I know, there is no such API. I think you could try to get the window size programmatically to determine if the screen is in Portrait.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by ChickenMythic Tuesday, September 02, 2014 7:25 AM
    Tuesday, September 02, 2014 2:27 AM
    Moderator
  • Thanks James,

    Determining orientation mode based on screen Width & Height would have been my B plan. I'll make our app support both Portrait & Landscape mode, only pages that allow the user to take pictures in Portrait mode would be affected by this issue. I'll figure something out... something that won't be that much (visually) disturbing for the end user...

    Thanks again.

    Tuesday, September 02, 2014 7:25 AM