使用加速计

了解如何使用加速计响应用户移动。

重要的 API

先决条件

你应熟悉 Extensible Application Markup Language (XAML)、Microsoft Visual C# 和事件。

你使用的设备或仿真器必须支持加速计。

创建简单的加速计应用

一个简单的游戏应用依赖于单个传感器,即加速计,作为输入设备。 这些应用通常只使用单轴或双轴进行输入,但它们也会将抖动事件作为另一个输入源使用。

注意

有关更完整的实现,请参阅加速计示例

Instructions

  • 创建新项目,从“Visual C#”项目模板中选择“空白应用(通用 Windows)”

  • 打开项目的 MainPage.xaml.cs 文件,使用下列内容替换现有代码。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;

    // Required to support the core dispatcher and the accelerometer

    using Windows.UI.Core;
    using Windows.Devices.Sensors;

    namespace App1
    {

        public sealed partial class MainPage : Page
        {
            // Sensor and dispatcher variables
            private Accelerometer _accelerometer;

            // This event handler writes the current accelerometer reading to
            // the three acceleration text blocks on the app' s main page.

            private async void ReadingChanged(object sender, AccelerometerReadingChangedEventArgs e)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    AccelerometerReading reading = e.Reading;
                    txtXAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
                    txtYAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
                    txtZAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);

                });
            }

            public MainPage()
            {
                this.InitializeComponent();
                _accelerometer = Accelerometer.GetDefault();

                if (_accelerometer != null)
                {
                    // Establish the report interval
                    uint minReportInterval = _accelerometer.MinimumReportInterval;
                    uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
                    _accelerometer.ReportInterval = reportInterval;

                    // Assign an event handler for the reading-changed event
                    _accelerometer.ReadingChanged += new TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>(ReadingChanged);
                }
            }
        }
    }

你需要使用你给予项目的名称重命名以上代码片段中的命名空间。 例如,如果你创建了一个名为AccelerometerCS的项目,则将 namespace App1 替换为 namespace AccelerometerCS

  • 打开文件 MainPage.xaml 并使用以下 XML 替换原始内容。
        <Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">

        <Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
            <TextBlock HorizontalAlignment="Left" Height="25" Margin="8,20,0,0" TextWrapping="Wrap" Text="X-axis:" VerticalAlignment="Top" Width="62" Foreground="#FFEDE6E6"/>
            <TextBlock HorizontalAlignment="Left" Height="27" Margin="8,49,0,0" TextWrapping="Wrap" Text="Y-axis:" VerticalAlignment="Top" Width="62" Foreground="#FFF5F2F2"/>
            <TextBlock HorizontalAlignment="Left" Height="23" Margin="8,80,0,0" TextWrapping="Wrap" Text="Z-axis:" VerticalAlignment="Top" Width="62" Foreground="#FFF6F0F0"/>
            <TextBlock x:Name="txtXAxis" HorizontalAlignment="Left" Height="15" Margin="70,16,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="61" Foreground="#FFF2F2F2"/>
            <TextBlock x:Name="txtYAxis" HorizontalAlignment="Left" Height="15" Margin="70,49,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="53" Foreground="#FFF2EEEE"/>
            <TextBlock x:Name="txtZAxis" HorizontalAlignment="Left" Height="15" Margin="70,80,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="53" Foreground="#FFFFF8F8"/>

        </Grid>
    </Page>

你将需要用你的应用的命名空间替换上面的代码片段中类名称的第一部分。 例如,如果你创建了一个名为AccelerometerCS的项目,则将 x:Class="App1.MainPage" 替换为 x:Class="AccelerometerCS.MainPage"。 还应当将 xmlns:local="using:App1" 替换为 xmlns:local="using:AccelerometerCS"

  • 按 F5 或依次选择“调试”>“开始调试”来生成、部署并运行应用。

应用运行后,可以通过移动设备或使用仿真器工具更改加速计的值。

  • 通过返回到 Visual Studio 并按 Shift+F5 或依次选择“调试”>“停止调试”来停止应用。

说明

前面的示例演示了,只需要写入极少的代码即可将加速计输入集成到你的应用。

该应用在 MainPage 方法中建立了与默认加速计的连接。

_accelerometer = Accelerometer.GetDefault();

该应用在 MainPage 方法中建立了报告间隔。 此代码检索设备支持的最短间隔,并将它与所请求的间隔 16 毫秒(大约 60-Hz 刷新率)进行比较。 如果支持的最短间隔大于所请求的间隔,则此代码会将报告间隔设置为所支持的最短间隔。 否则,它会将报告间隔设置为所请求的间隔。

uint minReportInterval = _accelerometer.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
_accelerometer.ReportInterval = reportInterval;

ReadingChanged 方法中捕获新的加速计数据。 每当传感器驱动程序从传感器接收到新数据时,它都将使用此事件处理程序将该值传递到你的应用中。 应用在下行中注册此事件处理程序。

_accelerometer.ReadingChanged += new TypedEventHandler<Accelerometer,
AccelerometerReadingChangedEventArgs>(ReadingChanged);

这些新值将写入位于项目 XAML 中的 TextBlock。

<TextBlock x:Name="txtXAxis" HorizontalAlignment="Left" Height="15" Margin="70,16,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="61" Foreground="#FFF2F2F2"/>
 <TextBlock x:Name="txtYAxis" HorizontalAlignment="Left" Height="15" Margin="70,49,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="53" Foreground="#FFF2EEEE"/>
 <TextBlock x:Name="txtZAxis" HorizontalAlignment="Left" Height="15" Margin="70,80,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="53" Foreground="#FFFFF8F8"/>