Detecting Orientation with Windows Phone 7

Detecting Orientation with Windows Phone 7

With the preview developer bits for building Windows Phone 7 applications now publically available I was experimenting with a few things and one of the first things I went looking for is support for handling device orientation. More specifically the event where the orientation of the device changes. Now you can actually do this two ways: you can either attach to the OrientationChanged event on the PhoneApplicationPage, or since your page inherits from this class you can override the OrientationChanged method. Note there is also an OrientationChanging event/method pair that you can also use.

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        { … }

OR

       protected override void OnOrientationChanged(OrientationChangedEventArgs e)
       {
           base.OnOrientationChanged(e);
           ….
       }

One of the interesting things to note is that if you are actually interested in whether the device is now in portrait or landscape you have to be careful how you test the new orientation. For example the following condition will probably never be true:

if (e.Orientation == PageOrientation.Portrait)

If you take a look at the PageOrientation enumeration you will see that it actually defines values for each orientation:

public enum PageOrientation
    {
        None = 0,
        Portrait = 1,
        Landscape = 2,
        PortraitUp = 5,
        PortraitDown = 9,
        LandscapeLeft = 24,
        LandscapeRight = 52,
    }

So the only way the previous condition would hold true is if the system decided to return an Orientation set to the more general Portrait, rather than the specifics of PortraitUp and PortraitDown.

Fine you say, it must be a flag in which case you have to “AND” it to work out whether it’s in Portrait or Landscape. Well yes, but you need to make sure that you only do that with the Portrait value. You will notice that the values associated with these values are not your typical flag values of 1, 2, 4, 8 etc. This means that when you AND say the Landscape value of 2 (binary 00010) with LandscapeLeft (binary 11000) you will get 0, when you were expecting to get a value of 2. Luckily when you are testing against Portrait you will notice that both PortraitUp (binary 00101) and PortraitDown (binary 01001) you will get the value 1, which is what you’d expect. So the code for doing different actions based on just the Portrait/Landscape orientation would look something like:

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
       {
           if ((e.Orientation & PageOrientation.Portrait)>0)
           {…}
           else
           {…}
       }

Perhaps someone out there can let me know why the enumeration has the values it does? Clearly I’m missing the obvious here, or perhaps this is a feature of the current preview bits.

Leave a Reply

Your email address will not be published. Required fields are marked *