54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace SHH.CameraDashboard
|
|
{
|
|
public enum ThemeType { Dark, Light }
|
|
|
|
public static class ThemeManager
|
|
{
|
|
public static void ChangeTheme(ThemeType theme)
|
|
{
|
|
var appResources = Application.Current.Resources;
|
|
|
|
// 1. 找到旧的颜色字典并移除
|
|
// 我们通过检查 Source 路径来识别它
|
|
ResourceDictionary oldDict = null;
|
|
foreach (var dict in appResources.MergedDictionaries)
|
|
{
|
|
// 只要路径里包含 "Colors." 说明它是我们的皮肤文件
|
|
if (dict.Source != null && dict.Source.OriginalString.Contains("Themes/Colors."))
|
|
{
|
|
oldDict = dict;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (oldDict != null)
|
|
{
|
|
appResources.MergedDictionaries.Remove(oldDict);
|
|
}
|
|
|
|
// 2. 加载新字典
|
|
string dictName = theme switch
|
|
{
|
|
ThemeType.Light => "/Style/Themes/Colors.Light.xaml",
|
|
ThemeType.Dark => "/Style/Themes/Colors.Dark.xaml",
|
|
_ => "/Style/Themes/Colors.Dark.xaml" // 默认
|
|
};
|
|
|
|
var newDict = new ResourceDictionary
|
|
{
|
|
Source = new Uri(dictName, UriKind.Relative)
|
|
};
|
|
|
|
// 3. 添加到集合中 (建议加在最前面,或者根据索引位置)
|
|
appResources.MergedDictionaries.Add(newDict);
|
|
}
|
|
}
|
|
}
|