첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.15MB

▶ MainPage.xaml

<?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>

 

728x90

 

▶ MainPage.xaml.cs

namespace TestProject;

/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - MainPage()

    /// <summary>
    /// 생성자
    /// </summary>
    public MainPage()
    {
        InitializeComponent();

        Accelerometer.ReadingChanged += accelerometer_ReadingChanged;
        this.startButton.Clicked     += startButton_Clicked;
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 가속도계 읽기 변경시 처리하기 - accelerometer_ReadingChanged(sender, e)

    /// <summary>
    /// 가속도계 읽기 변경시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private void accelerometer_ReadingChanged(object sender, AccelerometerChangedEventArgs e)
    {
        AccelerometerData data = e.Reading;

        this.label.Text = $"X축 : {data.Acceleration.X}\nY축 : {data.Acceleration.Y}\nZ축 : {data.Acceleration.Z}";
    }

    #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(Accelerometer.IsMonitoring)
        {
            Accelerometer.Stop();

            this.startButton.Text = "시작";

            this.label.Text = string.Empty;
        }
        else
        {
            this.startButton.Text = "중단";

            Accelerometer.Start(SensorSpeed.UI);
        }
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요