locked
Programmatic App Rotation & Orientation Change RRS feed

  • Question

  • I have a Windows Store App which has a height just a tiny bit too much for the 1366x768 landscape size (if I had just one or two less lines, it would fit). But I would like to allow as many users as possible to use it in landscape, and since it will fit many devices' landscape, I want to programmatically detect and change the orientation. How can I do this (detect the screen size and programmatically set the orientation)? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, July 6, 2014 12:58 AM

Answers

  • Are you suggesting to force your app into portrait orientation on 1366x768 displays? I would not recommend this, as it takes control away from the user and also will not work very well on non-tablet devices.

    The modern Windows 8 UI is deigned to be scalable. Why not simply modify your layout to work on all screens? There are many ways to scale, such as changing font size or using a viewbox to scale content.

    It sounds like your content *almost* fits on the screen, so why not consider reducing the margins?

    At the very least use a viewbox. I use one in one of my apps, and it does the job well enough.


    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.

    Monday, July 7, 2014 12:24 AM

All replies

  • The static Window.Current.Bounds property should get you the height and width of the application window:

    Rect bounds = Window.Current.Bounds;
    double height = bounds.Height;
    double width = bounds.Width;

    The Windows.Graphics.Display.DisplayProperties.CurrentOrientation (Windows.Graphics.Display.DisplayInformation.CurrentOrientation on Windows 8.1) property returns the current orientation:

    Windows.Graphics.Display.DisplayOrientations orientation = Windows.Graphics.Display.DisplayProperties.CurrentOrientation;

    You can set the preferred orientation using the Windows.Graphics.Display.DisplayProperties.AutoRotationPreferences property:

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

    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.

    Sunday, July 6, 2014 8:58 PM
  • That is definitely very useful information, but I am still having trouble getting my app to rotate when needed. First, I will just mention for everyone that I needed to use DisplayInformation instead of DisplayProperties, as Visual Studio 2013 told me (which was obviously simple to do). I tried using both of the following to change the rotation to Portrait when the height of my Grid was larger than the available screen height:
    Private Sub grdLayout_SizeChanged(sender As Object, e As SizeChangedEventArgs) Handles grdLayout.SizeChanged
    	If Me.grdLayout.RenderSize.Height > Window.Current.Bounds.Height AndAlso Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences <> DisplayOrientations.Portrait Then
    		Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait
    	End If
    End Sub
    
    Private Sub MainPage_SizeChanged(sender As Object, e As SizeChangedEventArgs) Handles Me.SizeChanged
    	If Me.grdLayout.RenderSize.Height > Window.Current.Bounds.Height AndAlso Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences <> DisplayOrientations.Portrait Then
    		Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait
    	End If
    End Sub
    When using the simulator with different resolutions, none of them rotated it, even though it was vertically too large for the current display. I even checked the values returned by each of the variables, and they are values that would make my If condition true. What am I doing wrong? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, July 6, 2014 10:15 PM
  • Are you suggesting to force your app into portrait orientation on 1366x768 displays? I would not recommend this, as it takes control away from the user and also will not work very well on non-tablet devices.

    The modern Windows 8 UI is deigned to be scalable. Why not simply modify your layout to work on all screens? There are many ways to scale, such as changing font size or using a viewbox to scale content.

    It sounds like your content *almost* fits on the screen, so why not consider reducing the margins?

    At the very least use a viewbox. I use one in one of my apps, and it does the job well enough.


    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.

    Monday, July 7, 2014 12:24 AM
  • If I put my content in a ViewBox, will I be able to have it automatically scale down when needed, and otherwise stay it's original size? That would be great, I have never used a ViewBox before. But because the text in this app (well, the text that I need scaled, anyway) is read-only (the user doesn't need to interact with it), having it a little smaller won't hurt anything, so automatic scaling would be great, if my understanding is correct from what I have heard about it, it might actually be better. Thanks, I might have never thought of that idea!

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, July 7, 2014 1:06 AM