Screen orientation and Change Handling for Windows Phone 7

Source: Internet
Author: User

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)
02 {
03     if(PageOrientation.LandscapeLeft == e.Orientation)
04     {
05         textBlock1.Text ="Horizontal (left )";
06     }
07     elseif(PageOrientation.LandscapeRight == e.Orientation)
08     {
09         textBlock1.Text ="Horizontal (right )";
10     }
11     else
12     {
13         textBlock1.Text ="Vertical";
14     }
15 }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.