了解如何使用加速计响应用户移动。
此示例创建一个简单的应用,该应用依赖于加速计作为输入设备。 你可以使用加速计在用户移动设备时做出响应。 基于加速计的应用通常只使用一个或两个轴进行输入。
- 重要 API:Windows.Devices.Sensors, Accelerometer
注释
本文重点介绍演示如何使用加速计的代码。 有关加速计传感器的概述,请参阅 传感器:加速计。
先决条件
你应该熟悉加速计传感器及其用途。 请参阅 传感器:加速计。
正在使用的设备必须支持加速计。
代码示例
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Controls;
using Windows.Devices.Sensors;
namespace DevicesDemo.Pages
{
public sealed partial class AccelerometerPage : Page
{
private Accelerometer? accelerometer;
public AccelerometerPage()
{
InitializeComponent();
// Get the default accelerometer sensor object.
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 += Accelerometer_ReadingChanged;
}
else
{
statusBar.Message = "No accelerometer was found.";
statusBar.Severity = InfoBarSeverity.Error;
statusBar.IsOpen = true;
}
}
// This event handler writes the current accelerometer reading to
// the three acceleration text blocks on the XAML page.
private void Accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
DispatcherQueue?.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
AccelerometerReading reading = args.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);
});
}
}
}
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Margin="24">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="44"/>
<RowDefinition Height="44"/>
<RowDefinition Height="44"/>
</Grid.RowDefinitions>
<TextBlock Text="X-axis:" Style="{StaticResource LabelTextBlockStyle}"/>
<TextBlock x:Name="txtXAxis" Grid.Column="1" Text="---"/>
<TextBlock Grid.Row="1" Text="Y-axis:" Style="{StaticResource LabelTextBlockStyle}"/>
<TextBlock x:Name="txtYAxis" Grid.Column="1" Grid.Row="1" Text="---"/>
<TextBlock Grid.Row="2" Text="Z-axis:" Style="{StaticResource LabelTextBlockStyle}"/>
<TextBlock x:Name="txtZAxis" Grid.Column="1" Grid.Row="2" Text="---"/>
</Grid>
<InfoBar x:Name="statusBar" Grid.Row="1"/>
</Grid>
应用运行时,可以通过移动设备来更改加速计值。
前面的示例演示了需要编写的基本代码,以便将加速计输入集成到应用中。
连接传感器
调用 GetDefault 方法以建立与默认加速计的连接。
private Accelerometer? accelerometer;
// ...
accelerometer = Accelerometer.GetDefault();
还可以调用 FromIdAsync 从 DeviceInformation.Id 值创建 Accelerometer 对象。 有关详细信息,请参阅 枚举设备。
如果未检测到加速计传感器,则会更新状态消息以通知用户。
设置报表间隔
报表间隔在页面的构造函数中设置。 此代码检索设备支持的最小间隔,并将其与请求的间隔 16 毫秒(大约为 60-Hz 刷新率)进行比较。 如果支持的最小间隔大于请求的间隔,则代码会将该值设置为最小值。 否则,它会将值设置为请求的间隔。
uint minReportInterval = accelerometer.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
accelerometer.ReportInterval = reportInterval;
读取传感器数据
新的加速计数据在 ReadingChanged 事件处理程序中捕获。 传感器驱动程序每次从传感器接收新数据时,都会使用此事件将值传递给应用。 对于此示例,这些新值将写入 XAML 中找到的相应页面的文本块。
accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
// ...
private void Accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
DispatcherQueue?.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
AccelerometerReading reading = args.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);
});
}
还可以将 Shaken 事件用作另一个输入源。 对 Shaken 事件的支持取决于硬件和驱动程序支持。 实际上,很少有加速计支持该 Shaken 事件。