Setting Page Orientation at Runtime
As you saw in the previous section, the Orientation
property of the PhoneApplicationPage
class cannot be assigned at runtime. Another approach is required to change the page orientation. For this turn to the SupportedOrientations
property, which is assignable at runtime.
To recap, the SupportedOrientations
property defines the allowed orientation or orientations of the page, which can be Portrait
, Landscape
, or PortraitOrLandscape
.
Setting the SupportedOrientations
property to the desired orientation forces the page to be shown in that
orientation. For example, if the page is being viewed in portrait
orientation, and you want to change the page orientation to landscape,
you can set the SupportedOrientations
to Landscape
, thereby restricting the allowed orientation to landscape and forcing the frame to change the orientation to landscape.
This technique is demonstrated in the OrientationView
page in the downloadable sample code (see Figure 2).
FIGURE 2 Forcing the orientation of a page.
The OrientationView
class’s Switch Supported Orientation button alternates between the three SupportedPageOrientation
values. This is performed in the button’s Tap
event handler, demonstrating how the orientation can be forcibly set by
restricting the orientation, as shown in the following excerpt:
void Button_Tap(object sender, GestureEventArgs e)
{
switch (SupportedOrientations)
{
case SupportedPageOrientation.Landscape:
SupportedOrientations = SupportedPageOrientation.Portrait;
break;
case SupportedPageOrientation.Portrait:
SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
break;
default:
SupportedOrientations = SupportedPageOrientation.Landscape;
break;
}
}