Click here to Skip to main content
15,867,594 members
Articles / Mobile Apps / Windows Phone 7

Support Multiple Orientations - Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
2.00/5 (1 vote)
12 Feb 2011CPOL 13K   3   1
Support Multiple Orientations - Windows Phone 7

If you worked on Windows Mobile before, you probably remember all the work that had to be done to support different orientations on your Windows Mobile application. You had to create different layouts/resources and detect the orientation change to load the corresponding layout. In Windows Phone7, this is much easier to handle.

By default, Silverlight applications for Windows Phone 7 run in portrait mode. To support both portrait and landscape mode, we just need to add an attribute in the root of your main page XAML file.

C#
SupportedOrientations="PortraitOrLandscape"

You can also restrict the orientation mode of your application by using the same attribute. For example, a game application (Silverlight) may be better viewed in landscape mode. In this case, the attribute would be set as follows:

C#
SupportedOrientations="Landscape"

For applications that get text input, it may not be enough to just set the orientation property because the hardware keyboard will take some of the display area. For this case, you may need to manipulate the layout by trapping the OrientationChanged event in the code.

In the XAML, we will need to add the event attribute as:

C#
OrientationChanged="PhoneApplicationPage_OrientationChanged"

In the code, we will write the handler as follows:

C#
private void PhoneApplicationPage_OrientationChanged
	(object sender, OrientationChangedEventArgs e)
{
    string orientation = e.Orientation.ToString();
    
    //add logic to handle your layout
    base.OnOrientationChanged(e);
}
This article was originally posted at http://ozkary.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect OG-BITechnologies
United States United States
Software engineer, author & speaker who enjoys mentoring, learning, speaking and sharing with others about software development technologies. Microsoft MVP.

My Blog

Comments and Discussions

 
GeneralMy vote of 2 Pin
h3213-Feb-11 5:23
h3213-Feb-11 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.