■ Compass 클래스의 ReadingChanged 정적 이벤트를 사용해 나침반 방향을 구하는 방법을 보여준다.
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"> <Grid RowDefinitions="*,Auto,10,Auto,*"> <Button x:Name="startButton" Grid.Row="1" HorizontalOptions="Center" WidthRequest="100" Text="시작" /> <Label x:Name="label" Grid.Row="3" HorizontalOptions="Center" /> </Grid> </ContentPage> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); Compass.ReadingChanged += compass_ReadingChanged; this.startButton.Clicked += startButton_Clicked; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 나침반 읽기 변경시 처리하기 - compass_ReadingChanged(sender, e) /// <summary> /// 나침반 읽기 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void compass_ReadingChanged(object sender, CompassChangedEventArgs e) { CompassData data = e.Reading; this.label.Text = $"북쪽 방향 : {data.HeadingMagneticNorth}도"; } #endregion #region 시작 버튼 클릭시 처리하기 - startButton_Clicked(sender, e) /// <summary> /// 시작 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void startButton_Clicked(object sender, EventArgs e) { if(Compass.IsMonitoring) { Compass.Stop(); this.startButton.Text = "시작"; this.label.Text = string.Empty; } else { this.startButton.Text = "중단"; Compass.Start(SensorSpeed.UI, true); } } #endregion } |