When the direction of our mobile phone changes, some events are actually triggered. Both phoneapplicationframe and phoneapplicationpage contain the oritentationchanged event. Phoneapplicationpage also adds a convenient equivalent method onoritentationchanged, which is protected and Rewritable. The following example demonstrates how to rewrite the onoritentationchanged method in the project and display the direction of the current mobile phone in a textblock.
XAML code:
<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock Name="txtblk" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
C # code:
Public partial class mainpage: phoneapplicationpage
{
// Constructor
Public mainpage ()
{
Initializecomponent ();
Txtblk. Text = This. Orientation. tostring (); // gets the value of the orientation attribute of the current page.
}
Protected override void onorientationchanged (orientationchangedeventargs e) // rewrite the virtual method onorientationchanged of the base class phoneapplicationpage
{
Txtblk. Text = E. Orientation. tostring (); // get the new value of the attribute orientation after the direction is changed.
Base. onorientationchanged (E); // call the onorientationchanged method of the base class
}
}
Effect
Vertical direction horizontal direction
The preceding example shows that you can modify the application direction not only through the supportedorientations attribute in the XAML code, but also through the C # code modification page oritentation. In addition, when the mobile phone direction changes, the program will trigger the orientationchanged event accordingly. We can use this event to handle some things.