學習如何使用羅盤來判斷當前航向。
這個例子建立了一個簡單的應用程式,依賴指南針作為輸入裝置。 應用程式可以取得相對於磁北(或真北)的當前航向。 導航應用程式利用羅盤判斷裝置面向的方向,並相應地定位地圖。
- 重要 API:Windows。裝置.感測器,羅盤
Note
本文重點介紹示範如何使用指南針的程式碼。 關於羅盤感測器的概述,請參見 「感測器:羅盤」。
Prerequisites
你應該熟悉指南針感測器及其用途。 參見 感測器:指南針。
你使用的裝置或模擬器必須支援指南針。
範例程式碼
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.";
});
}