47、SimpleOrientationSensor

 

 

    Win 8 设备方向的枚举 :

namespace Windows.Devices.Sensors
{
    // 指示设备的方向。
    public enum SimpleOrientation
    {
       // 设备未旋转。这对应于上纵向。
        NotRotated = 0,
     
       // 设备逆时针旋转 90 度。这对应于左横向。
        Rotated90DegreesCounterclockwise = 1,
       
       // 设备逆时针旋转 180 度。这对应于下纵向。
        Rotated180DegreesCounterclockwise = 2,
       
       // 设备逆时针旋转 270 度。这对应于右横向。
        Rotated270DegreesCounterclockwise = 3,
        
       // 将设备面朝上并且显示对用户不可见。
        Faceup = 4,      

       // 将设备面朝下并且显示对用户隐藏。
        Facedown = 5,
    }
}

 

 

1、Simple orientation sensor data events:

     在页面的 C# 页面声明一个全局变量:

  // 表示简单的方向传感器。
  private SimpleOrientationSensor _sensor;

 

在页面的构造函数中,初始化这个传感器对象:

// 获取简单的默认方向传感器。
_sensor = SimpleOrientationSensor.GetDefault();
if (_sensor == null)
{
      // 没有找到系统的简单传感器
}
else
{
  //在 Visible 属性的值更改时发生。
  Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
  _sensor.OrientationChanged += new TypedEventHandler<SimpleOrientationSensor, 
SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged); }

 

在当前窗口不可见时,不需要侦听窗口方向的变化:

        private void VisibilityChanged(object sender, VisibilityChangedEventArgs e)
        {
            if (ScenarioDisableButton.IsEnabled)
            {
                if (e.Visible)
                {
                   //窗口可见时,侦听
                    _sensor.OrientationChanged += new TypedEventHandler<SimpleOrientationSensor, 
SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged); } else { // 窗口不可见时不再侦听 _sensor.OrientationChanged -= new TypedEventHandler<SimpleOrientationSensor,
SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged); } } }

 


当窗口方向发生改变时,调用:

async private void OrientationChanged(object sender, SimpleOrientationSensorOrientationChangedEventArgs e)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                switch (e.Orientation)
               {
                case SimpleOrientation.NotRotated:
                      //  Not Rotated
                    break;
                case SimpleOrientation.Rotated90DegreesCounterclockwise:
                      // Rotated 90 Degrees Counterclockwise
                    break;
                case SimpleOrientation.Rotated180DegreesCounterclockwise:
                       // Rotated 180 Degrees Counterclockwise
                    break;
                case SimpleOrientation.Rotated270DegreesCounterclockwise:
                     // Rotated 270 Degrees Counterclockwise
                    break;
                case SimpleOrientation.Faceup:
                    // Faceup
                    break;
                case SimpleOrientation.Facedown:
                    // Facedown
                    break;
                default:
                     // Unknown orientation
                    break;
            }
            });
        }


当页面导航离开时,取消事件侦听:

 protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {            
                Window.Current.VisibilityChanged -= new WindowVisibilityChangedEventHandler(VisibilityChanged);
                _sensor.OrientationChanged -= new TypedEventHandler<SimpleOrientationSensor, 
SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged); base.OnNavigatingFrom(e); }

 

2、 Current simple  orientation sensor reading :

      执行一个请求来检索当前设备的方向。

      在按钮事件中,处理当前设备方向:

private SimpleOrientationSensor _sensor;

private void Button_Click(object sender, RoutedEventArgs e)
{
      _sensor = SimpleOrientationSensor.GetDefault();
    if (_sensor != null)
    {
       // 获取 默认简单的方向传感器的方向。
       switch (_sensor.GetCurrentOrientation())
     {
     case SimpleOrientation.NotRotated:
          // Not Rotated
         break;
     case SimpleOrientation.Rotated90DegreesCounterclockwise:
          // Rotated 90 Degrees Counterclockwise
         break;
     case SimpleOrientation.Rotated180DegreesCounterclockwise:
           //  Rotated 180 Degrees Counterclockwise
         break;
     case SimpleOrientation.Rotated270DegreesCounterclockwise:
            // Rotated 270 Degrees Counterclockwise
         break;
     case SimpleOrientation.Faceup:
           //  Faceup
         break;
     case SimpleOrientation.Facedown:
           //  Facedown
         break;
     default:
           // Unknown orientation
         break;

       }
    }
}

 

你可能感兴趣的:(ORIENTATION)