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.
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:
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:
OrientationChanged="PhoneApplicationPage_OrientationChanged"
In the code, we will write the handler as follows:
private void PhoneApplicationPage_OrientationChanged
(object sender, OrientationChangedEventArgs e)
{
string orientation = e.Orientation.ToString();
base.OnOrientationChanged(e);
}