mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Add chapter 10 sample project
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using System.Reflection;
|
||||
using System.Windows.Input;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
using TemplateStudioSampleApp.Contracts.Services;
|
||||
using TemplateStudioSampleApp.Helpers;
|
||||
|
||||
using Windows.ApplicationModel;
|
||||
|
||||
namespace TemplateStudioSampleApp.ViewModels;
|
||||
|
||||
public partial class SettingsViewModel : ObservableRecipient
|
||||
{
|
||||
private readonly IThemeSelectorService _themeSelectorService;
|
||||
|
||||
[ObservableProperty]
|
||||
private ElementTheme _elementTheme;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _versionDescription;
|
||||
|
||||
public ICommand SwitchThemeCommand
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public SettingsViewModel(IThemeSelectorService themeSelectorService)
|
||||
{
|
||||
_themeSelectorService = themeSelectorService;
|
||||
_elementTheme = _themeSelectorService.Theme;
|
||||
_versionDescription = GetVersionDescription();
|
||||
|
||||
SwitchThemeCommand = new RelayCommand<ElementTheme>(
|
||||
async (param) =>
|
||||
{
|
||||
if (ElementTheme != param)
|
||||
{
|
||||
ElementTheme = param;
|
||||
await _themeSelectorService.SetThemeAsync(param);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static string GetVersionDescription()
|
||||
{
|
||||
Version version;
|
||||
|
||||
if (RuntimeHelper.IsMSIX)
|
||||
{
|
||||
var packageVersion = Package.Current.Id.Version;
|
||||
|
||||
version = new(packageVersion.Major, packageVersion.Minor, packageVersion.Build, packageVersion.Revision);
|
||||
}
|
||||
else
|
||||
{
|
||||
version = Assembly.GetExecutingAssembly().GetName().Version!;
|
||||
}
|
||||
|
||||
return $"{"AppDisplayName".GetLocalized()} - {version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user