Forcing Orientation in WinRT

7d703de7-03a1-4a9b-8425-841ae065c30fI am working with a client on an enterprise Win8 app that is for order taking. They have a specific page that they require to be only in Portrait mode while the rest of the app can support any orientation. Since I've done so much Windows Phone 7/8 work I thought this would be simple. Just specify the value on the Page. But this didn't work…

Digging through the docs I found a probable solution: DisplayProperties.AutoRotationPreferences (in the Windows.Graphics.Display namespace). The docs specify that this property can be set with the DisplayOrientations enumeration to specify which of the four orientations to support. The enumeration is a flag so you can combine them too:

// All orientations
DisplayProperties.AutoRotationPreferences = DisplayOrientations.None;

// Portrait only
DisplayProperties.AutoRotationPreferences = DisplayOrientations.Portrait;

// Landscape only
DisplayProperties.AutoRotationPreferences = DisplayOrientations.Landscape;

// Landscape and upside down landscape only
DisplayProperties.AutoRotationPreferences = DisplayOrientations.Landscape | 
                                            DisplayOrientations.LandscapeFlipped;

Using these options should work, right? I tested it in a bunch of different places in my code and it didn't seem to have any effect. I was baffled.

Later I found an MSDN forum posting where they mentioned these work but only if the device has an accelerometer. This means on my laptop and even in the simulator these have *no* effect! When I did remote debugging to my Surface…it worked exactly the way I wanted it to work. Whew…the client never knew that I had to look it up.  Unless he reads my blog that is ;)

 


Leave a Comment

*
*
*