了解如何使用指南针确定当前标题。
此示例创建一个简单的应用,该应用依赖于指南针作为输入设备。 应用可以获取相对于磁北或真北的当前航向。 导航应用使用指南针确定设备面向的方向,然后相应地定位地图。
- 重要 API:Windows.Devices.Sensors、Compass
注释
本文重点介绍演示如何使用指南针的代码。 有关指南针传感器的概述,请参阅 传感器:指南针。
先决条件
你应该熟悉指南针传感器及其用法。 请参阅 传感器:指南针。
你正在使用的设备或模拟器必须支持指南针。
代码示例
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Controls;
using Windows.Devices.Sensors;
namespace DevicesDemo.Pages
{
public sealed partial class CompassPage : Page
{
private Compass? compass;
public CompassPage()
{
InitializeComponent();
// Get the default compass object.
compass = Compass.GetDefault();
if (compass != null)
{
// Establish the report interval for all scenarios.
uint minReportInterval = compass.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
compass.ReportInterval = reportInterval;
// Assign an event handler for the reading-changed event.
compass.ReadingChanged += Compass_ReadingChanged;
}
else
{
statusBar.Message = "No compass was found.";
statusBar.Severity = InfoBarSeverity.Error;
statusBar.IsOpen = true;
}
}
// This event handler writes the current compass
// reading to the text blocks on the XAML page.
private void Compass_ReadingChanged(Compass sender, CompassReadingChangedEventArgs args)
{
DispatcherQueue?.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
CompassReading reading = args.Reading;
txtMagnetic.Text = String.Format("{0,5:0.00}", reading.HeadingMagneticNorth);
if (reading.HeadingTrueNorth.HasValue)
txtNorth.Text = String.Format("{0,5:0.00}", reading.HeadingTrueNorth);
else
txtNorth.Text = "No reading.";
});
}
}
}
<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"/>
</Grid.RowDefinitions>
<TextBlock Text="Magnetic Heading:" Style="{StaticResource LabelTextBlockStyle}"/>
<TextBlock x:Name="txtMagnetic" Grid.Column="1" Text="---"/>
<TextBlock Grid.Row="1" Text="True North Heading:" Style="{StaticResource LabelTextBlockStyle}"/>
<TextBlock x:Name="txtNorth" Grid.Column="1" Grid.Row="1" Text="---"/>
</Grid>
<InfoBar x:Name="statusBar" Grid.Row="1"/>
</Grid>
应用运行时,可以通过移动设备来更改指南针值。
前面的示例演示了需要编写的基本代码,以便将指南针输入集成到应用中。
连接传感器
调用 GetDefault 方法以建立与默认指南针的连接。
private Compass? compass;
// ...
compass = Compass.GetDefault();
还可以调用 FromIdAsync,从 DeviceInformation.Id 值创建 compass 对象。 有关详细信息,请参阅 枚举设备。
如果未检测到指南针传感器,则会更新状态消息以通知用户。
设置报表间隔
报表间隔在页面的构造函数中设置。 此代码检索设备支持的最小间隔,并将其与请求的间隔 16 毫秒(大约为 60-Hz 刷新率)进行比较。 如果支持的最小间隔大于请求的间隔,则代码会将该值设置为最小值。 否则,它会将值设置为请求的间隔。
uint minReportInterval = compass.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
compass.ReportInterval = reportInterval;
读取传感器数据
新的指南针数据在 ReadingChanged 事件处理程序中捕获。 传感器驱动程序每次从传感器接收新数据时,都会使用此事件将值传递给应用。 对于此示例,这些新值将写入 XAML 中找到的相应页面的文本块。
compass.ReadingChanged += Compass_ReadingChanged;
// ...
private void Compass_ReadingChanged(Compass sender, CompassReadingChangedEventArgs args)
{
DispatcherQueue?.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
CompassReading reading = args.Reading;
txtMagnetic.Text = String.Format("{0,5:0.00}", reading.HeadingMagneticNorth);
if (reading.HeadingTrueNorth.HasValue)
txtNorth.Text = String.Format("{0,5:0.00}", reading.HeadingTrueNorth);
else
txtNorth.Text = "No reading.";
});
}