65.9K
CodeProject 正在变化。 阅读更多。
Home

Windows Phone 8 加速度传感器

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2013年2月20日

CPOL

2分钟阅读

viewsIcon

22607

downloadIcon

788

在WP8应用程序中实现加速度传感器。

引言

通过本文,您将了解使用加速度传感器开发基于传感器的Windows Phone 8应用程序的基本概念。

背景

Windows Phone 8支持多种传感器来确定设备的运动和方向,例如加速度传感器、指南针传感器、陀螺仪和组合运动。 通过在应用程序中使用这些传感器,物理设备本身就可以充当用户输入。

加速度传感器允许应用程序实时捕获沿X、Y和Z轴的加速度。 因此,加速度传感器可以报告施加在其上的力的方向和大小。 加速度值表示为三维向量,代表X(水平)、Y(垂直)和Z(垂直于基准面)轴上的加速度分量。 每个维度的值可以在重力单位中从'-2'到'2'范围内变化。

使用代码

让我们看看它的实现:

  1. 打开VS2012并选择 Windows Phone 类别,然后创建一个新的Windows Phone项目。
  2. 为了使用传感器,请将 Microsoft.Devices.SensorsMicrosoft.Xna.Framework 引用添加到您的项目中。 另外,请确保在项目属性文件夹下的App manifest文件中勾选 ID_CAP_SENSORS 功能。
  3. 现在这是我的用户界面,我这里有两个按钮来启动和停止捕获幅度,以及一个Pivot控件。 因此,当手机向右倾斜时,具有一定的幅度,Pivot元素的索引应该改变,并导航到下一个项目。 以下是XAML:
  4. //XAML   
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock Text="Simple Sensor Application" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
    <StackPanel Orientation="Horizontal">
    <Button Content="Start Sensor" Click="Button_Click_1" 
      Height="80" Width="200" HorizontalAlignment="Left"/>
    <Button Content="Stop Sensor" Click="Button_Click_2" 
      Height="80" Width="200" HorizontalAlignment="Left"/>
    </StackPanel>
    </StackPanel>
    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <phone:Pivot Name="pivot">
    <phone:PivotItem Header="Item1"> <TextBlock>AAAA</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item2"> <TextBlock>BBBB</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item3"> <TextBlock>CCCC</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item4"> <TextBlock>DDDD</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item5"> <TextBlock>EEEE</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item6"> <TextBlock>FFFF</TextBlock> </phone:PivotItem>
    </phone:Pivot>
    </Grid>
  5. UI准备就绪,现在我们希望在单击“开始”按钮时开始捕获施加的力的大小。 Accelerometer 是提供x、y、z轴值的API,以 SensorReading 的形式在 CurrentValueChanged 回调中提供。 在此示例中,加速度传感器为我们提供每 5 秒的更新,分配给 TimeBetweenUpdates 属性。
  6. 单击“开始”按钮

    // Instantiate the Accelerometer.
    accelerometer = new Accelerometer();
    accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(500);
    accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged);
    accelerometer.Start();

    CurrentValueChanged 回调处理程序

    void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
    { 
        Vector3 acceleration = e.SensorReading.Acceleration;
        // If user tilts the phone with 0.35 in X axis, Handle/Change the pivot index
        if (acceleration.X <= -0.35)
        {
            // Application Logic
           // Change the pivot control index by -1
        }
        if (acceleration.X >= 0.35)
        {
            // Application Logic
            // change the pivot control index by +1
        }
    }
  7. 单击“停止”按钮以停止来自加速度传感器的更新。 调用加速度传感器的 Stop 方法。
    accelerometer.Stop();

您可以下载附带的源代码。

© . All rights reserved.