WP7屏幕方向笔记

本文详细介绍了如何在 Windows Phone 应用程序中通过设置 SupportedOrientation 属性来实现支持转屏功能,并通过检测 Orientation 属性来判断设备当前的方向(竖直或水平)。同时,文章还讲解了如何在屏幕旋转时自动调整控件布局,以保持良好的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        一般应用程序默认是只有竖屏状态,通过修改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#代码中改位置的格式。还可以添加条件智能的修改控件的位置。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值