■ RelayCommand<T> 클래스를 사용해 메소드나 람다 표현식을 실행하는 방법을 보여준다.
※ CommunityToolkit.Mvvm 누겟을 설치한다.
▶ 예제 코드 (C#)
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 |
using System.Reflection; using System.Windows.Input; using Windows.ApplicationModel; using Microsoft.UI.Xaml; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; namespace TestProject; /// <summary> /// 설정 뷰 모델 /// </summary> public partial class SettingViewModel : ObservableRecipient { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 테마 셀렉터 서비스 /// </summary> private readonly IThemeSelectorService themeSelectorService; /// <summary> /// 엘리먼트 테마 /// </summary> [ObservableProperty] private ElementTheme elementTheme; /// <summary> /// 버전 설명 /// </summary> [ObservableProperty] private string versionDescription; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 스위치 테마 명령 - SwitchThemeCommand /// <summary> /// 스위치 테마 명령 /// </summary> public ICommand SwitchThemeCommand { get; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SettingViewModel(themeSelectorService) /// <summary> /// 생성자 /// </summary> /// <param name="themeSelectorService">테마 셀렉터 서비스</param> public SettingViewModel(IThemeSelectorService themeSelectorService) { this.themeSelectorService = themeSelectorService; this.elementTheme = this.themeSelectorService.Theme; this.versionDescription = GetVersionDescription(); SwitchThemeCommand = new RelayCommand<ElementTheme> ( async (parameter) => { if(ElementTheme != parameter) { ElementTheme = parameter; await this.themeSelectorService.SetThemeAsync(parameter); } } ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 버전 설명 구하기 - GetVersionDescription() /// <summary> /// 버전 설명 구하기 /// </summary> /// <returns>버전 설명</returns> private static string GetVersionDescription() { Version version; if(RuntimeHelper.IsMSIX) { PackageVersion packageVersion = Package.Current.Id.Version; version = new(packageVersion.Major, packageVersion.Minor, packageVersion.Build, packageVersion.Revision); } else { version = Assembly.GetExecutingAssembly().GetName().Version!; } return $"{"AppDisplayName".GetLocalString()} - {version.Major}.{version.Minor}.{version.Build}.{version.Revision}"; } #endregion } |