Windows Phone 十九、罗盘

时间:2024-12-28 13:07:26
磁力计概述

拥有磁力计硬件支持的设备可以根据磁力计来确定相对于北极的角度

磁力计的访问 API 定义在 Compass 类中

调用方式和加速计类似

     <Grid Background="White">
<Image Source="compass.png" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<CompositeTransform x:Name="rotate" Rotation="1"/>
</Image.RenderTransform>
</Image>
</Grid>
         protected override void OnNavigatedTo(NavigationEventArgs e)
{
Compass c = Compass.GetDefault();
if (c == null)
{
// 不支持罗盘
return;
}
c.ReadingChanged += async (p1, p2) =>
{
// 修改图片的旋转角度;
System.Diagnostics.Debug.WriteLine(p2.Reading.HeadingTrueNorth.Value);
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
rotate.Rotation = -p2.Reading.HeadingTrueNorth.Value;
});
};
c.ReportInterval = c.MinimumReportInterval * ;
}