We all know that smartphones can be rotated to change the display direction of the screen, more time, for the screen direction of change, we have to do the appropriate processing, for example, when the phone screen orientation from portrait to landscape, you may want to rearrange the controls on the page to accommodate the changes in the display area.
As we discussed earlier, the page layout of the Silverlight for Windows phone has three common layout controls, so when the screen orientation changes, the changes we make to the layout are based on the actions of these containers. In this article, I will explain each of the three examples. Before you begin, talk about the orientationchanged event of the PhoneApplicationPage class, which occurs when the orientation of the screen changes. We get the direction of the current screen from the Orientation property of the instance of the event argument Orientationchangedeventargs class, that is, the direction of the change, for example, the original screen is portrait, and now I change the screen of the phone to landscape, Then the Orientation property gets to the direction is horizontal, hehe, of course, also includes from which direction rotation comes, here is just an example.
To make the page support rotation, change the PhoneApplicationPage supportedorientations property to Portraitorlandscape, and then you can handle the layout by defining the Orientationchanged event. Shaped like:
- <phone:phoneapplicationpage
- ..............
- Supportedorientations= "Portraitorlandscape"
- orientation= "Portrait"
- orientationchanged= "Phoneapplicationpage_orientationchanged" >
One, the processing of the grid control.
- <phone:phoneapplicationpage
- X:class= "Sample_pagedir.page1"
- Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
- Xmlns:phone= "Clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"
- Xmlns:shell= "Clr-namespace:microsoft.phone.shell;assembly=microsoft.phone"
- Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
- Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
- Fontfamily= "{StaticResource phonefontfamilynormal}"
- Fontsize= "{StaticResource phonefontsizenormal}"
- Foreground= "{StaticResource Phoneforegroundbrush}"
- mc:ignorable= "D" d:designheight= "768" d:designwidth= "480"
- Shell:systemtray.isvisible= "True"
- Supportedorientations= "Portraitorlandscape"
- orientation= "Portrait"
- orientationchanged= "Phoneapplicationpage_orientationchanged" >
- <grid x:name= "LayoutRoot" >
- <Grid.RowDefinitions>
- <rowdefinition height= "Auto"/>
- <rowdefinition height= "Auto"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <columndefinition width= "Auto"/>
- <columndefinition width= "Auto"/>
- </Grid.ColumnDefinitions>
- <image x:name= "img" source= "http://gubapic.eastmoney.com/member/e68/e681999/e68199920091216131540.jpg" Stretch = "UniformToFill" width= "grid.row=" 0 "grid.column=" 0 "/>
- <textblock x:name= "Txtblock"
- grid.row= "1"
- grid.column= "0"
- Fontsize= "70"
- Margin= ">"
- <run foreground= "Coral" > Xin chun </Run>
- <LineBreak/>
- <run foreground= "Yellow" > Singing love songs </Run>
- <LineBreak/>
- <run foreground= "Skyblue" > Non-hanging section </Run>
- </TextBlock>
- </Grid>
- </phone:PhoneApplicationPage>
The main page has two controls, one for displaying pictures, one for displaying text information, and for changing the layout of two controls with event-handling code.
- private void Phoneapplicationpage_orientationchanged (object sender, Orientationchangedeventargs e)
- {
- If it's horizontal,
- if (e.orientation = = Pageorientation.landscape | |
- E.orientation = = Pageorientation.landscapeleft | |
- E.orientation = = pageorientation.landscaperight)
- {
- Grid.setcolumn (this.img, 0);
- Grid.setrow (this.img, 0);
- Grid.setrow (this.txtblock, 0);
- Grid.setcolumn (This.txtblock, 1);
- }
- If it is portrait
- else if (e.orientation = = Pageorientation.portrait | |
- E.orientation = = Pageorientation.portraitdown | |
- E.orientation = = Pageorientation.portraitup)
- {
- Grid.setcolumn (this.img, 0);
- Grid.setrow (this.img, 0);
- Grid.setrow (This.txtblock, 1);
- Grid.setcolumn (this.txtblock, 0);
- }
- Else
- {
- Grid.setcolumn (this.img, 0);
- Grid.setrow (this.img, 0);
- Grid.setrow (This.txtblock, 1);
- Grid.setcolumn (this.txtblock, 0);
- }
- }
Press F5 to run the program, the default screen orientation is portrait, as shown in:
OK, now, let's rotate the screen to see what happens.
Second, the processing of StackPanel control.
- <phone:phoneapplicationpage
- X:class= "Sample_pagedir.page2"
- Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
- Xmlns:phone= "Clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"
- Xmlns:shell= "Clr-namespace:microsoft.phone.shell;assembly=microsoft.phone"
- Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
- Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
- Fontfamily= "{StaticResource phonefontfamilynormal}"
- Fontsize= "{StaticResource phonefontsizenormal}"
- Foreground= "{StaticResource Phoneforegroundbrush}"
- Supportedorientations= "Portraitorlandscape"
- Orientationchanged= "Phoneapplicationpage_orientationchanged"
- orientation= "Portrait"
- mc:ignorable= "D" d:designheight= "768" d:designwidth= "480"
- shell:systemtray.isvisible= "True" >
- <phone:PhoneApplicationPage.Resources>
- <style targettype= "TextBlock" >
- <setter property= "FontSize" value= "/>"
- </Style>
- </phone:PhoneApplicationPage.Resources>
- <stackpanel x:name= "PL" >
- <textblock text= "text One"/>
- <textblock text= "text two"/>
- <textblock text= "text three"/>
- </StackPanel>
- </phone:PhoneApplicationPage>
Background event handling code.
- private void Phoneapplicationpage_orientationchanged (object sender, Orientationchangedeventargs e)
- {
- if (e.orientation = = Pageorientation.landscape | |
- E.orientation = = Pageorientation.landscapeleft | |
- E.orientation = = pageorientation.landscaperight)
- {
- This.pl.Orientation = System.Windows.Controls.Orientation.Horizontal;
- }
- Else
- {
- This.pl.Orientation = System.Windows.Controls.Orientation.Vertical;
- }
- }
Run, the default orientation is portrait.
Turn the screen back on.
Third, the processing of the canvas control.
- <phone:phoneapplicationpage
- X:class= "Sample_pagedir.page3"
- Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
- Xmlns:phone= "Clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"
- Xmlns:shell= "Clr-namespace:microsoft.phone.shell;assembly=microsoft.phone"
- Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
- Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
- Fontfamily= "{StaticResource phonefontfamilynormal}"
- Fontsize= "{StaticResource phonefontsizenormal}"
- Foreground= "{StaticResource Phoneforegroundbrush}"
- Supportedorientations= "Portraitorlandscape"
- orientation= "Portrait"
- Orientationchanged= "Phoneapplicationpage_orientationchanged"
- mc:ignorable= "D" d:designheight= "768" d:designwidth= "480"
- shell:systemtray.isvisible= "True" >
- <canvas x:name= "CV" >
- <rectangle x:name= "Rect1"
- Width= "232"
- Height= "238"
- Fill= "Red"
- canvas.left= "88"
- Canvas.top= "/>"
- <rectangle x:name= "Rect2"
- Height= "192"
- Width= "275"
- Fill= "Yellow"
- canvas.top= "268"
- canvas.left= "155"/>
- </Canvas>
- </phone:PhoneApplicationPage>
Background code. Background code.
- private void Phoneapplicationpage_orientationchanged (object sender, Orientationchangedeventargs e)
- {
- if (e.orientation== pageorientation.landscape| | e.orientation== pageorientation.landscapeleft| | e.orientation== pageorientation.landscaperight)
- {
- Canvas.settop (This.rect1, 37);
- Canvas.setleft (This.rect1, 46);
- Canvas.settop (This.rect2, 240);
- Canvas.setleft (This.rect2, 462);
- }
- Else
- {
- Canvas.settop (This.rect1, 88);
- Canvas.setleft (This.rect1, 88);
- Canvas.settop (This.rect2, 268);
- Canvas.setleft (This.rect2, 155);
- }
- }
Look at the effect. Look at the effect.
Longitudinal.
Transverse.
Windows Phone Development (6): Handling Changes in screen orientation