■ CommunityToolkit MVVM 패턴에서 명령에 인자를 전달하는 방법을 보여준다.
▶ MainPageViewModel.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 |
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; namespace TestProject { /// <summary> /// 메인 페이지 뷰 모델 /// </summary> [INotifyPropertyChanged] public partial class MainPageViewModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 입력 텍스트 /// </summary> [ObservableProperty] private string inputText = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPageViewModel() /// <summary> /// 생성자 /// </summary> public MainPageViewModel() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 초기화하기 - Reset() /// <summary> /// 초기화하기 /// </summary> [RelayCommand] private void Reset() { InputText = string.Empty; } #endregion #region 시간 표시하기 - ShowTime(format) /// <summary> /// 시간 표시하기 /// </summary> /// <param name="format">포맷 문자열</param> [RelayCommand] private void ShowTime(string format) { InputText = DateTime.Now.ToString(format); } #endregion } } |
▶ MainPage.xaml
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 |
<?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:MainPageViewModel /> </ContentPage.BindingContext> <VerticalStackLayout HorizontalOptions="Center" VerticalOptions="Center" Spacing="10"> <Entry HorizontalOptions="Center" Placeholder="텍스트를 입력해 주시기 바랍니다." FontSize="16" Text="{Binding InputText}" /> <Button HorizontalOptions="Center" Text="초기화" Command="{Binding ResetCommand}" /> <Button HorizontalOptions="Center" Text="시간 표시" CommandParameter="HH:mm:ss" Command="{Binding ShowTimeCommand}" /> </VerticalStackLayout> </ContentPage> |