■ ICommand 인터페이스를 사용하는 방법을 보여준다.
▶ KeypadViewModel.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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
using System.ComponentModel; using System.Windows.Input; namespace TestProject; /// <summary> /// 키패드 뷰 모델 /// </summary> public class KeypadViewModel : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 입력 문자열 /// </summary> private string inputString = string.Empty; /// <summary> /// 디스플레이 텍스트 /// </summary> private string displayText = string.Empty; /// <summary> /// 특수 문자 배열 /// </summary> private char[] specialCharacterArray = { '*', '#' }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자 추가 명령 - AddCharacterCommand /// <summary> /// 문자 추가 명령 /// </summary> public ICommand AddCharacterCommand { get; private set; } #endregion #region 문자 삭제 명령 - DeleteCharacterCommand /// <summary> /// 문자 삭제 명령 /// </summary> public ICommand DeleteCharacterCommand { get; private set; } #endregion #region 입력 문자열 - InputString /// <summary> /// 입력 문자열 /// </summary> public string InputString { get { return this.inputString; } private set { if(this.inputString != value) { this.inputString = value; FirePropertyChangedEvent("InputString"); DisplayText = GetFormatText(this.inputString); ((Command)DeleteCharacterCommand).ChangeCanExecute(); } } } #endregion #region 디스플레이 텍스트 - DisplayText /// <summary> /// 디스플레이 텍스트 /// </summary> public string DisplayText { get { return this.displayText; } private set { if(this.displayText != value) { this.displayText = value; FirePropertyChangedEvent("DisplayText"); } } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - KeypadViewModel() /// <summary> /// 생성자 /// </summary> public KeypadViewModel() { AddCharacterCommand = new Command<string> ( (key) => { InputString += key; } ); DeleteCharacterCommand = new Command ( () => { InputString = InputString.Substring(0, InputString.Length - 1); }, () => { return InputString.Length > 0; } ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="propertyName">속성명</param> protected void FirePropertyChangedEvent(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 포맷 텍스트 구하기 - GetFormatText(source) /// <summary> /// 포맷 텍스트 구하기 /// </summary> /// <param name="source">소스 문자열</param> /// <returns>포맷 텍스트</returns> private string GetFormatText(string source) { bool hasNonNumbers = source.IndexOfAny(this.specialCharacterArray) != -1; string target = source; if(hasNonNumbers || source.Length < 4 || source.Length > 10) { } else if(source.Length < 8) { target = string.Format ( "{0}-{1}", source.Substring(0, 3), source.Substring(3) ); } else { target = string.Format ( "({0}) {1}-{2}", source.Substring(0, 3), source.Substring(3, 3), source.Substring(6) ); } return target; } #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 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<?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.Resources> <Style TargetType="Button"> <Setter Property="FontSize" Value="Large" /> </Style> </ContentPage.Resources> <ContentPage.BindingContext> <local:KeypadViewModel /> </ContentPage.BindingContext> <Grid HorizontalOptions="Center" VerticalOptions="Center"> <Grid.RowDefinitions> <RowDefinition Height="100" /> <RowDefinition Height="100" /> <RowDefinition Height="100" /> <RowDefinition Height="100" /> <RowDefinition Height="100" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="100" /> <ColumnDefinition Width="100" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Margin="5" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="HeadTruncation" FontSize="32" Text="{Binding DisplayText}" /> <Button Grid.Row="0" Grid.Column="2" Margin="5" Text="<" Command="{Binding DeleteCharacterCommand}" /> <Button Grid.Row="1" Grid.Column="0" Margin="5" CommandParameter="1" Command="{Binding AddCharacterCommand}" Text="1" /> <Button Grid.Row="1" Grid.Column="1" Margin="5" Text="2" CommandParameter="2" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="1" Grid.Column="2" Margin="5" Text="3" CommandParameter="3" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="2" Grid.Column="0" Margin="5" Text="4" CommandParameter="4" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="2" Grid.Column="1" Margin="5" Text="5" CommandParameter="5" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="2" Grid.Column="2" Margin="5" Text="6" CommandParameter="6" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="3" Grid.Column="0" Margin="5" Text="7" CommandParameter="7" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="3" Grid.Column="1" Margin="5" Text="8" CommandParameter="8" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="3" Grid.Column="2" Margin="5" Text="9" CommandParameter="9" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="4" Grid.Column="0" Margin="5" Text="*" CommandParameter="*" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="4" Grid.Column="1" Margin="5" Text="0" CommandParameter="0" Command="{Binding AddCharacterCommand}" /> <Button Grid.Row="4" Grid.Column="2" Margin="5" Text="#" CommandParameter="#" Command="{Binding AddCharacterCommand}" /> </Grid> </ContentPage> |