This article briefly introduces the screen direction of Windows Phone 7 and how to handle the screen direction change.
The screen direction of Windows Phone 7 is divided into vertical and horizontal directions. However, unlike Windows Mobile, the screen direction of Windows Phone 7 is different from that of Windows Mobile. It is clear in the figure:
1. vertical direction:
The vertical direction does not support 180 degrees of rotation. Think about it too. Few people are used to turning their phones upside down.
2. Horizontal Direction:
In the horizontal direction, you must note that the horizontal direction is divided into the left horizontal direction and the Right horizontal direction. This is because the ApplicationBar (Application toolbar) at the lower part of the screen changes in this direction, applicationBar is next to the three buttons on the phone:
Left horizontal direction Right horizontal direction
Note that the direction of the system tray has also changed, that is, the one that shows the time.
In general, the program should consider the page layout in the screen direction. To handle the response, pay attention to the differences between the left and right horizontal directions.
When you add a new page to a project, you can select the screen direction:
The phoneapplicationpage attribute orientation can set the initial direction of the application page, but this attribute cannot be set in the code. I tried it and can set this attribute in the code, however, the screen direction has not changed. So how can I adapt the UI to screen rotation? Set the supportedorientations attribute of phoneapplicationpage, but do not modify the display mode of the screen according to the code.
Supportedpageorientation. Landscape supports horizontal orientation
Supportedpageorientation. Portrait supports vertical orientation
Supportedpageorientation. portraitorlandscape supports horizontal and vertical directions (the page automatically selects the corresponding page view when the phone is rotated)
Example
The following example shows how to set and change the screen direction:
Create a Windows Phone 7 Application,
First, add a textblock to the page and set its text attribute to vertical.
1 |
<TextBlockHeight="36"HorizontalAlignment="Left"Margin="150,127,0,0"Name="textBlock1"Text="Vertical"VerticalAlignment="Top"TextAlignment="Center"FontSize="30"Width="152"/> |
Compile and execute the command. The interface is as follows:
Click the rotate button on the right to rotate the screen. Currently, no processing is performed, and the interface will not be adaptive to the horizontal status. The interface is as follows:
By default, the supportedorientations attribute of mainpagepage is set to portrait.
In XAML, set the supportedorientations attribute to portraitorlandscape.
1 |
SupportedOrientations="PortraitOrLandscape" |
Compile and execute it again. It's easy. Just one line of code will enable the screen to rotate automatically. The page is as follows:
The current screen is already in the horizontal state and you want to handle it. For example, if "horizontal" is displayed in textblock, you need to handle the rotation event.
Add the orientationchanged event to the page.
01 |
privatevoidPhoneApplicationPage_OrientationChanged(objectsender, OrientationChangedEventArgs e) |
03 |
if(PageOrientation.LandscapeLeft == e.Orientation) |
05 |
textBlock1.Text ="Horizontal (left )"; |
07 |
elseif(PageOrientation.LandscapeRight == e.Orientation) |
09 |
textBlock1.Text ="Horizontal (right )"; |
13 |
textBlock1.Text ="Vertical"; |
In this way, the text in textblock changes accordingly. Operations related to screen rotation can be processed in the event response.
The interface is as follows:
Code download