Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Page orientation not changing? There is a solution for that!

DZone's Guide to

Page orientation not changing? There is a solution for that!

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Today I was introducing a fellow developre to the fun world of Windows Phone 7 development. In the process of explaining the basic principles, we reached the topic of changing page orientations. And when it came to the point where I had to provide a working sample of a page that reacts to a change orientations, I noticed that my sample failed.

In some cases when you work on a Windows Phone 7 application on your developer machine and you debug your application in the emulator, you might experience an unexpected consequence to a change in page orientation. To be specific - you will see no effect at all when you use the rotation buttons in the emulator. The OrientationChanged event handler is never triggered.

The first thing I did is check the properties set for the page that define the page orientation:

SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"

No problems here. I decided to set a breakpoint on the event handler, so that I can track exactly when the orientation changes. But it never reached that.

So I decided to take it further and experiment with device rotation in XNA.

This is what I had in the Game() constructor:

graphics.SupportedOrientations = DisplayOrientation.Default | DisplayOrientation.LandscapeLeft
| DisplayOrientation.LandscapeRight | DisplayOrientation.Portrait;

Window.OrientationChanged += new EventHandler<EventArgs>(Window_OrientationChanged);

The event handler was so simple that I don't even have to explain it's purpose:

void Window_OrientationChanged(object sender, EventArgs e)
{
Debug.WriteLine("Changed");
}

The results were a surprise to me:

The orientation was changing in an XNA game and not in a Silverlight-based application. It did seem to me that this is a framework-related bug. However, I noticed that sometimes it appeared and sometimes it didn't. What I've noticed is that there are two workarounds available for this problem.

The first one, you need to override the default OnOrientationChanged for the page:

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

And it worked - the OrientationChanged event handler started working. Another solution would be forcing the page to change the orientation manually, but this is only a solution when you want to manually trigger the change and isn't really reliable (you might need to tie it to the Accelerometer - and you won't be able to test it in the emulator).

What needs to be done is directly enforcing the SupportedOrientation limit:

this.SupportedOrientations = SupportedPageOrientation.Landscape;
this.Orientation = PageOrientation.LandscapeLeft;

If you've encountered this issue and have another workaround, let me know and I will update this post.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}