How to limit orientation in metro apps?
Asked Answered
A

3

7

In my app, all of the stuff is only in Landscape mode. I don't want the app to be functional in Portrait mode. How do I limit the orientation?

Thanks.

Armistead answered 27/9, 2012 at 5:14 Comment(4)
By not doing subscribing for the orientation changed event (not doing anything when the orientation changes).Shebat
In your appmanifest file, you can limit the supported orientations.Jewel
Thanks for the reply, I have tried using limiting stuff in Appmanifest. But when I rotate my screen and see them, they become a bit weird in Portrait mode.Armistead
This link has some great stuff on managing orientation (though not limiting it): markermetro.com/2011/11/technical/…Claar
C
4

As explained in this link the orientation limitation preference setting of the app is only enforced on a Windows 8 system with a supported HARDWARE ACCELEROMETER. This means that unless Windows knows how the system is orientated through the means of a supported sensor, it will not attempt to switch to the app's preferred orientation.

So it will all depend on the user's hardware.

Claar answered 27/9, 2012 at 8:10 Comment(0)
S
4

I had this problem as well as I wanted to constrain my game to only landscape mode. I put this in my OnLaunched handler for App.xaml:

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

However I noted that in the simulator it seemed to ignore this whereas on the hardware tablet I tested on it seemed to behave appropriately. The AutoRotationPreferences are bit flags so you can or together all the orientations you want to allow.

Selway answered 16/10, 2012 at 16:47 Comment(1)
simulator tip off saved me timeMonafo
K
0

For people looking to answer this question who aren't writing a Metro app (where you can set preferred orientations in the manifest or have access to Windows.Graphics.Display.DisplayProperties.AutoRotationPreferences)...

There is no real way to NOT let the Orientation change, however if you are interested in only allowing Landscape you could do something like this:

View Model:

Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new           
    EventHandler(SystemEvents_DisplaySettingsChanged);
}

public bool IsLandscape { get; set; }

void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
  if (SystemParameters.PrimaryScreenWidth > SystemParameters.PrimaryScreenHeight)
  {
      IsLandscape = true;
  }
  else
  {
      IsLandscape = false;
  }

  RaisePropertyChanged( "IsLandscape" );
}

In you Main Window.xaml:

<Border >
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsLandscape}" Value="False">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <RotateTransform Angle="90"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
         </Style>
    </Border.Style>

///The rest of your controls and UI

</Border>

So we really aren't limiting the Orientation, we are just noticing when it happens, and re rotating our UI so it still looks like it is in Portrait mode :) Again this is mostly for non Metro Win 8 applications and or applications that also run on Win 7 tablets.

Koons answered 23/10, 2014 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.