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

TestProject.zip
0.15MB

▶ MainViewModel.cs

using System.ComponentModel;
using System.Windows.Input;

namespace TestProject;

/// <summary>
/// 메인 뷰 모델
/// </summary>
public class MainViewModel : INotifyPropertyChanged
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Event
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 속성 변경시 - PropertyChanged

    /// <summary>
    /// 속성 변경시
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 값
    /// </summary>
    private double value = 1;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 값 - Value

    /// <summary>
    /// 값
    /// </summary>
    public double Value
    {
        get
        {
            return this.value;
        }
        set
        {
            if(this.value != value)
            {
                this.value = value;

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
            }
        }
    }

    #endregion

    #region 곱하기 명령 - MultiplyCommand

    /// <summary>
    /// 곱하기 명령
    /// </summary>
    public ICommand MultiplyCommand { get; private set; }

    #endregion
    #region 나누기 명령 - DivideCommand

    /// <summary>
    /// 나누기 명령
    /// </summary>
    public ICommand DivideCommand { get; private set; }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - MainViewModel()

    /// <summary>
    /// 생성자
    /// </summary>
    public MainViewModel()
    {
        MultiplyCommand = new Command(() => Value *= 2);
        DivideCommand   = new Command(() => Value /= 2);
    }

    #endregion
}

 

728x90

 

▶ 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"
    xmlns:local="clr-namespace:TestProject">
    <ContentPage.BindingContext>
        <local:MainViewModel />
    </ContentPage.BindingContext>
    <StackLayout
        HorizontalOptions="Center"
        VerticalOptions="Center">
        <Label
            HorizontalOptions="Center"
            FontSize="18"
            Text="{Binding Value, StringFormat='값 : {0}'}" />
        <Button
            HorizontalOptions="Center"
            Margin="0,10,0,0"
            Text="2로 곱하기"
            Command="{Binding MultiplyCommand}" />
        <Button
            HorizontalOptions="Center"
            Margin="0,10,0,0"
            Text="2로 나누기"
            Command="{Binding DivideCommand}" />
    </StackLayout>
</ContentPage>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요