磁力计概述
拥有磁力计硬件支持的设备可以根据磁力计来确定相对于北极的角度
磁力计的访问 API 定义在 Compass 类中
调用方式和加速计类似
1 <Grid Background="White"> 2 <Image Source="compass.png" RenderTransformOrigin="0.5,0.5"> 3 <Image.RenderTransform> 4 <CompositeTransform x:Name="rotate" Rotation="1"/> 5 </Image.RenderTransform> 6 </Image> 7 </Grid>
1 protected override void OnNavigatedTo(NavigationEventArgs e) 2 { 3 Compass c = Compass.GetDefault(); 4 if (c == null) 5 { 6 // 不支持罗盘 7 return; 8 } 9 c.ReadingChanged += async (p1, p2) => 10 { 11 // 修改图片的旋转角度; 12 System.Diagnostics.Debug.WriteLine(p2.Reading.HeadingTrueNorth.Value); 13 await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => 14 { 15 rotate.Rotation = -p2.Reading.HeadingTrueNorth.Value; 16 }); 17 }; 18 c.ReportInterval = c.MinimumReportInterval * 10; 19 }
时间: 2024-11-05 13:45:02