您的位置:首页 > 其它

Windows 8 应用开发权威指南 之 检测方向的传感器(3)确定设备方向

2013-04-19 10:44 615 查看

确定设备方向

SimpleOrientation传感器可以判断当前设备纵向向上、纵向向下、横向向左和横向向右的方向以及设备的正面向上或正面向下的状态。使用它可以实现屏幕自动旋转,无需通过软件或硬件按钮就可以控制旋转屏幕。下面通过示例介绍SimpleOrientation传感器API的使用。

新建一个Windows应用商店的空白应用程序项目,并命名为SimpleOrientationExample,在MainPage.xaml文件的Grid元素中添加如下代码。

<StackPanel HorizontalAlignment="Left" Margin="10">

<TextBlock Text="设备方向" Height="40" FontSize="20"/>

<TextBlock x:Name="ScenarioOutput_Orientation" Width="200" Height="50" FontSize="18"/>

<!--控制按钮-->

<Button Name="GetDataButton" Content="获得设备方向" Click="GetData_Click" />

<Button Name="CloseButton" Content="停止" Click="CloseData_Click" Width="121"/>

<TextBlock x:Name="InfoText" Width="200" Height="40" />

</StackPanel>

在上面的代码中,放置了3个显示信息的TextBlock控件,其中有一个用于显示SimpleOrientation传感器数据。然后又添加了2个按钮,并为这2个按钮定义了事件处理方法,一个用于启动SimpleOrientation传感器数据获取,另外一个按钮则用于停止数据获取。

前台运行效果如图12-11所示。





图12-11显示设备方向的界面

布局好前台界面后,接下来介绍如何从SimpleOrientation传感器获取读数。首先在后台定义几个全局变量并在构造方法中初始化,以便在后续代码中使用。代码如下所示:

//定义全局变量

private SimpleOrientationSensor sensor;

public MainPage()

{

this.InitializeComponent();

//获得对设备方向传感器对象的引用

sensor = SimpleOrientationSensor.GetDefault();

CloseButton.IsEnabled = false;

}

在上面的代码中,使用SimpleOrientationSensor.GetDefault方法获得SimpleOrientation传感器的引用对象sensor,并设置“停止”按钮为不可用状态。

定义全局变量后,接下来对“获取设备方向”按钮添加GetData_Click事件处理方法,单击这个按钮以启动SimpleOrientation传感器读数捕获。代码如下所示:

private void GetData_Click(object sender, RoutedEventArgs e)

{

if (sensor != null)

{

//注册处理程序OrientationChanged

sensor.OrientationChanged += new TypedEventHandler<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>(GetChangedData);

CloseButton.IsEnabled = true;

GetDataButton.IsEnabled = false;

}

else

{

InfoText.Text = "未发现SimpleOrientation传感器设备";

}

}

在上面的代码中,为sensor对象的OrientationChanged事件添加事件处理方法GetChangedData,当SimpleOrientation传感器驱动程序捕获到设备方向改变时,会触发OrientationChanged事件进而执行GetChangedData方法,下面来看一下GetChangedData方法的实现代码。

在GetChangedData方法中,定义了获取SimpleOrientation传感器数据并将数据显示到前台界面的过程,具体代码如下所示:

async private void GetChangedData (object sender, SimpleOrientationSensorOrientationChangedEventArgs e)

{

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>

{

//处理获得的设备方向

switch (e.Orientation)

{

case SimpleOrientation.NotRotated:

ScenarioOutput_Orientation.Text = "没有旋转";

break;

case SimpleOrientation.Rotated90DegreesCounterclockwise:

ScenarioOutput_Orientation.Text = "逆时针旋转90度";

break;

case SimpleOrientation.Rotated180DegreesCounterclockwise:

ScenarioOutput_Orientation.Text = "逆时针旋转180度";

break;

case SimpleOrientation.Rotated270DegreesCounterclockwise:

ScenarioOutput_Orientation.Text = "逆时针旋转270度";

break;

case SimpleOrientation.Faceup:

ScenarioOutput_Orientation.Text = "朝上的";

break;

case SimpleOrientation.Facedown:

ScenarioOutput_Orientation.Text = "朝下的";

break;

default:

ScenarioOutput_Orientation.Text = "不确定的方向";

break;

}

});

}

在上面的方法中,根据参数e的Orientation属性获得一个值,如果这个值等于枚举类型SimpleOrientation中的成员NotRotated,就在ScenarioOutput_Orientation文本框中显示“没有旋转”,然后分别设置e. Orientation等于其他值时ScenarioOutput_Orientation文本框的显示内容,枚举类型SimpleOrientation中的成员与设备方向的对应情况如表12-6所示。

表12-6 SimpleOrientation的成员与设备方向的对应关系

枚举类型SimpleOrientation的成员

设备的方向

NotRotated

没有旋转

Rotated90DegreesCounterclockwise

逆时针旋转90度

Rotated180DegreesCounterclockwise

逆时针旋转180度

Rotated270DegreesCounterclockwise

逆时针旋转270度

Faceup

屏幕朝上

Facedown

屏幕朝下

为“停止”按钮添加单击事件处理方法CloseData_Click,当单击此按钮时停止获取SimpleOrientation传感器数据,CloseData_Click方法的代码如下所示:

private void CloseData_Click(object sender, RoutedEventArgs e)

{

if (sensor != null)

{

sensor.OrientationChanged -= new TypedEventHandler<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>( GetChangedData);

CloseButton.IsEnabled = false;

GetDataButton.IsEnabled = true;

}

else

{

InfoText.Text = "关闭传感器失败";

}

}

在上面的代码中,停止捕获的原理与停止指南针传感器数据的捕获相似,请参照12.2.1节的原理介绍。

启动调试,单击“获取设备方向”按钮后,“获取设备方向”按钮将转变为不可用状态,界面上显示当前SimpleOrientation传感器的方向。“停止”按钮变为可激活状态。

单击“停止”按钮,将停止获取SimpleOrientation传感器方向信息,此时“停止”按钮变为不可用,“获取设备方向”按钮变为可激活状态。程序运行效果如图12-12所示。





图12-12 显示设备方向的界面

SimpleOrientationSensor类有一个GetCurrentOrientation方法,该方法能获得当前SimpleOrientation传感器的方向信息,返回值类型为SimpleOrientation,参照主动获取陀螺仪读数的部分,也可以使用计时器每隔一段时间主动获取一次当前设备SimpleOrientation传感器的方向信息。

注:本文选自机械工业出版社3月出品的《Windows 8 应用开发权威指南》第12章 传感器
另外:为在4月27日举办的微软云体验营免费活动做一下宣传。

微软云体验营- 北京站 ,4月27日免费开营啦!名额有限速速报名!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐