Files
FT479/WPFWCSDemo/ViewModels/Settings/SkinViewModel.cs
2026-06-29 14:46:32 +08:00

78 lines
2.0 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
namespace WPFWCSDemo.ViewModels.Settings
{
public partial class SkinViewModel:ObservableObject
{
[ObservableProperty]
static Frame frame;
public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches;
//深浅色切换
private bool _isDarkTheme;
public bool IsDarkTheme
{
get => _isDarkTheme;
set
{
if (SetProperty(ref _isDarkTheme, value))
{
ModifyTheme(theme => theme.SetBaseTheme(value ? Theme.Dark : Theme.Light));
}
}
}
//切换主题
private static void ModifyTheme(Action<ITheme> modificationAction)
{
var paletteHelper = new PaletteHelper();
ITheme theme = paletteHelper.GetTheme();
modificationAction?.Invoke(theme);
paletteHelper.SetTheme(theme);
}
private readonly PaletteHelper paletteHelper = new PaletteHelper();
/// <summary>
/// 改变颜色
/// </summary>
/// <param name="obj"></param>
[RelayCommand]
public void ChangeHue(object? obj)
{
var hue = (Color)obj!;
ITheme theme = paletteHelper.GetTheme();
theme.PrimaryLight = new ColorPair(hue.Lighten());
theme.PrimaryMid = new ColorPair(hue);
theme.PrimaryDark = new ColorPair(hue.Darken());
paletteHelper.SetTheme(theme);
}
public static void GetFrame(Frame navigationFrame)
{
frame = navigationFrame;
}
}
}