WP7屏幕方向笔记

来源:互联网 发布:linux 安装tar 编辑:程序博客网 时间:2024/05/20 02:23

        一般应用程序默认是只有竖屏状态,通过修改PhoneApplication的SupportedOrientation属性设置为PortraitOrLandscape,程序就支持转屏了。

 

SupportedOrientation = "PortraitOrLandscape"               XAML

SupportedOrientation = SupportedPageOrientation.PortraitOlrLandscape          C#

方向检测

       通过PhoneApplicationPage的Orientation属性来检测方向,PhoneApplicationPage回返回一个Orientation的枚举中的一个值。

 public enum PageOrientation

{

          None = 0,

          Portrait = 1,

          Landscape = 2,

          PortraitUp = 5,

          PortraitDown = 9,

          LandscapeLeft = 18,

          LandscapleRight = 34

}

     大多数情况下,查询Orientation属性确定设备处于竖直模式还是水平模式,但不必关注如LandscapLeft 和 LandscapRight 模式之间的差异,用switch语句就能方便的做到这一点:

 

   var orientation = this.Otientation;

   switch (orientation)

{

         case :  PageOrientation.Portrait:

         case :  PageOrientation.PortraitUp:

         case :  PageOrientation.PortraitDown:

             MessageBox.Show("Portrait");

             break;

         case :  PageOrientation.Landscape:

         case :  PageOrientation.LandscapeLeft:

         case :  PageOrientation.LandscapeRight:

               MessageBox.Show("Landscape");

               break;

}

 

还可以使用二进制AND语句来测试特定的方向值,下面的代码使用IsOrientation 方法将Orientation 与 textOrientation  的值进行“与”运算来测试结果是否大于零:

 

public static class Utilities {  public static bool IsInLandscape (this PhoneApplicationPage page)  {   return !page.IsInportrait();  }  public static bool IsInPortrait(this PhoneApplicationPage page)  {   return page.IsInOrientation(PageOrientation.Portrait)|PageOrientation.PortraitUp|PageOrientation.PortraitDown);  }  public static bool IsInOrientation(this PhoneApplicaionPage page,PageOrientation testOrientation)  {   var orientaion = page.Orientation;   return page.Orientation.IsOrientation(testOrientation);  }  public static bool IsOrientation (this PageOrientation orientation,PageOrientation testOrientation)  {   return (orienation &testOrientation)>0;  } }


000000   None = 0

000001    Portrait = 1

000010    Landscape = 2

000101    PortraitUp = 5

001001    PortraitDown = 9

010010    LandscapeLeft = 18

100010    LandscapeRight = 34

 

 

需要注意的是 Portration、PortrationUp、PortrationDown的最低位均为1.相反Landscape、LandscapeLift、LandsapeRight的第二位为1。次属性在IsOrientation方法中用来测试方向是Portration还是Landspace。例如,如果testOrientation的值是Landscape,当orientation的值等于Landscape、LandscapeLift和LandscapeRight时,IsOrientation方法将返回true。

 

 

方向更改

 

Windows Phone 通过OrientationChanged事件表明设备方向的变化。当用户改变设备方向时,会引发OrientationChanged事件。可以通过两种方法之一来关联方向事件。或者重写基类的OnOrientationChanged方法。在Visual Studio总,如果选择添加事件处理程序,可在Document Outline窗口中选择PhoneApplicationPage,然后从Properties窗口中选择相应的事件。双击事件旁边的空白单元格即可创建并关联事件处理程序。例如下面所示的OrientationChanged事件处理程序:

private void PhoneApplicationPage_OrientationChanged(object sender,OrientationChangedEventArgs e){...}
 
另一种方法是重写基类的方法。在本例中,等效方法是OnOrientation,可以按如下所示将其重写
protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
    base.OnOrientationChanged(e);
}

 

自动布局

更改页面布局可以调整控件的水平,垂直单面距离在转动屏幕的时候还是会根据对应单面垂直的距离来决定位置,例如对上距离是10,对左距离是20,则在转换屏的时候优先考虑上下的距离。

Button.verticalAlignment = VerticalAlignment.Bottom;

TopLefitButton.Margin = new Thickness(20,0,0,20);

TopLefiBuyyon.HorizontalAlignment = HorizontalAignment.Left;

TopLefiButton.Margin = new Thickness(20,20,0,0);

 

 

以上代码只是为了说名在C#代码中改位置的格式。还可以添加条件智能的修改控件的位置。