Windows Phone Development (6): Handling Changes in screen orientation

Source: Internet
Author: User

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:

    1. <phone:phoneapplicationpage
    2. ..............
    3. Supportedorientations= "Portraitorlandscape"
    4. orientation= "Portrait"
    5. orientationchanged= "Phoneapplicationpage_orientationchanged" >

One, the processing of the grid control.
  1. <phone:phoneapplicationpage
  2. X:class= "Sample_pagedir.page1"
  3. Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
  5. Xmlns:phone= "Clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"
  6. Xmlns:shell= "Clr-namespace:microsoft.phone.shell;assembly=microsoft.phone"
  7. Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
  8. Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. Fontfamily= "{StaticResource phonefontfamilynormal}"
  10. Fontsize= "{StaticResource phonefontsizenormal}"
  11. Foreground= "{StaticResource Phoneforegroundbrush}"
  12. mc:ignorable= "D" d:designheight= "768" d:designwidth= "480"
  13. Shell:systemtray.isvisible= "True"
  14. Supportedorientations= "Portraitorlandscape"
  15. orientation= "Portrait"
  16. orientationchanged= "Phoneapplicationpage_orientationchanged" >
  17. <grid x:name= "LayoutRoot" >
  18. <Grid.RowDefinitions>
  19. <rowdefinition height= "Auto"/>
  20. <rowdefinition height= "Auto"/>
  21. </Grid.RowDefinitions>
  22. <Grid.ColumnDefinitions>
  23. <columndefinition width= "Auto"/>
  24. <columndefinition width= "Auto"/>
  25. </Grid.ColumnDefinitions>
  26. <image x:name= "img" source= "http://gubapic.eastmoney.com/member/e68/e681999/e68199920091216131540.jpg" Stretch = "UniformToFill" width= "grid.row=" 0 "grid.column=" 0 "/>
  27. <textblock x:name= "Txtblock"
  28. grid.row= "1"
  29. grid.column= "0"
  30. Fontsize= "70"
  31. Margin= ">"
  32. <run foreground= "Coral" > Xin chun </Run>
  33. <LineBreak/>
  34. <run foreground= "Yellow" > Singing love songs </Run>
  35. <LineBreak/>
  36. <run foreground= "Skyblue" > Non-hanging section </Run>
  37. </TextBlock>
  38. </Grid>
  39. </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.

  1. private void Phoneapplicationpage_orientationchanged (object sender, Orientationchangedeventargs e)
  2. {
  3. If it's horizontal,
  4. if (e.orientation = = Pageorientation.landscape | |
  5. E.orientation = = Pageorientation.landscapeleft | |
  6. E.orientation = = pageorientation.landscaperight)
  7. {
  8. Grid.setcolumn (this.img, 0);
  9. Grid.setrow (this.img, 0);
  10. Grid.setrow (this.txtblock, 0);
  11. Grid.setcolumn (This.txtblock, 1);
  12. }
  13. If it is portrait
  14. else if (e.orientation = = Pageorientation.portrait | |
  15. E.orientation = = Pageorientation.portraitdown | |
  16. E.orientation = = Pageorientation.portraitup)
  17. {
  18. Grid.setcolumn (this.img, 0);
  19. Grid.setrow (this.img, 0);
  20. Grid.setrow (This.txtblock, 1);
  21. Grid.setcolumn (this.txtblock, 0);
  22. }
  23. Else
  24. {
  25. Grid.setcolumn (this.img, 0);
  26. Grid.setrow (this.img, 0);
  27. Grid.setrow (This.txtblock, 1);
  28. Grid.setcolumn (this.txtblock, 0);
  29. }
  30. }


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.
  1. <phone:phoneapplicationpage
  2. X:class= "Sample_pagedir.page2"
  3. Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
  5. Xmlns:phone= "Clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"
  6. Xmlns:shell= "Clr-namespace:microsoft.phone.shell;assembly=microsoft.phone"
  7. Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
  8. Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. Fontfamily= "{StaticResource phonefontfamilynormal}"
  10. Fontsize= "{StaticResource phonefontsizenormal}"
  11. Foreground= "{StaticResource Phoneforegroundbrush}"
  12. Supportedorientations= "Portraitorlandscape"
  13. Orientationchanged= "Phoneapplicationpage_orientationchanged"
  14. orientation= "Portrait"
  15. mc:ignorable= "D" d:designheight= "768" d:designwidth= "480"
  16. shell:systemtray.isvisible= "True" >
  17. <phone:PhoneApplicationPage.Resources>
  18. <style targettype= "TextBlock" >
  19. <setter property= "FontSize" value= "/>"
  20. </Style>
  21. </phone:PhoneApplicationPage.Resources>
  22. <stackpanel x:name= "PL" >
  23. <textblock text= "text One"/>
  24. <textblock text= "text two"/>
  25. <textblock text= "text three"/>
  26. </StackPanel>
  27. </phone:PhoneApplicationPage>


Background event handling code.

  1. private void Phoneapplicationpage_orientationchanged (object sender, Orientationchangedeventargs e)
  2. {
  3. if (e.orientation = = Pageorientation.landscape | |
  4. E.orientation = = Pageorientation.landscapeleft | |
  5. E.orientation = = pageorientation.landscaperight)
  6. {
  7. This.pl.Orientation = System.Windows.Controls.Orientation.Horizontal;
  8. }
  9. Else
  10. {
  11. This.pl.Orientation = System.Windows.Controls.Orientation.Vertical;
  12. }
  13. }


Run, the default orientation is portrait.

Turn the screen back on.

Third, the processing of the canvas control.
  1. <phone:phoneapplicationpage
  2. X:class= "Sample_pagedir.page3"
  3. Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
  5. Xmlns:phone= "Clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"
  6. Xmlns:shell= "Clr-namespace:microsoft.phone.shell;assembly=microsoft.phone"
  7. Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
  8. Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. Fontfamily= "{StaticResource phonefontfamilynormal}"
  10. Fontsize= "{StaticResource phonefontsizenormal}"
  11. Foreground= "{StaticResource Phoneforegroundbrush}"
  12. Supportedorientations= "Portraitorlandscape"
  13. orientation= "Portrait"
  14. Orientationchanged= "Phoneapplicationpage_orientationchanged"
  15. mc:ignorable= "D" d:designheight= "768" d:designwidth= "480"
  16. shell:systemtray.isvisible= "True" >
  17. <canvas x:name= "CV" >
  18. <rectangle x:name= "Rect1"
  19. Width= "232"
  20. Height= "238"
  21. Fill= "Red"
  22. canvas.left= "88"
  23. Canvas.top= "/>"
  24. <rectangle x:name= "Rect2"
  25. Height= "192"
  26. Width= "275"
  27. Fill= "Yellow"
  28. canvas.top= "268"
  29. canvas.left= "155"/>
  30. </Canvas>
  31. </phone:PhoneApplicationPage>


Background code. Background code.

  1. private void Phoneapplicationpage_orientationchanged (object sender, Orientationchangedeventargs e)
  2. {
  3. if (e.orientation== pageorientation.landscape| | e.orientation== pageorientation.landscapeleft| | e.orientation== pageorientation.landscaperight)
  4. {
  5. Canvas.settop (This.rect1, 37);
  6. Canvas.setleft (This.rect1, 46);
  7. Canvas.settop (This.rect2, 240);
  8. Canvas.setleft (This.rect2, 462);
  9. }
  10. Else
  11. {
  12. Canvas.settop (This.rect1, 88);
  13. Canvas.setleft (This.rect1, 88);
  14. Canvas.settop (This.rect2, 268);
  15. Canvas.setleft (This.rect2, 155);
  16. }
  17. }


Look at the effect. Look at the effect.

Longitudinal.

Transverse.

Windows Phone Development (6): Handling Changes in screen orientation

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.