WPF 动态切换黑|白皮肤
WPF 使用 WPFDevelopers.Minimal如何动态切换黑|白皮肤
作者:WPFDevelopersOrg
原文链接: https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal
框架使用 大于等于.NET40;
Visual Studio 2022;
项目使用 MIT开源许可协议;
Nuget Install-Package WPFDevelopers.Minimal 3.2.6-preview
新建白天资源文件 Light.Color.xaml;
<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"po:Freeze="True">
<!--字体颜色-->
<Colorx:Key="PrimaryTextColor"po:Freeze="True">#303133 </Color>
<SolidColorBrushx:Key="PrimaryTextSolidColorBrush"Color="{StaticResource PrimaryTextColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="RegularTextColor"po:Freeze="True">#606266 </Color>
<SolidColorBrushx:Key="RegularTextSolidColorBrush"Color="{StaticResource RegularTextColor}"po:Freeze="True"></SolidColorBrush>
<!--背景色-->
<Colorx:Key="BackgroundColor"po:Freeze="True">#FFFFFF </Color>
<SolidColorBrushx:Key="BackgroundSolidColorBrush"Color="{StaticResource BackgroundColor}"po:Freeze="True"></SolidColorBrush>
<SolidColorBrushx:Key="WindowForegroundColorBrush"Color="{StaticResource BackgroundColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="BaseColor"po:Freeze="True">#DCDFE6 </Color>
<SolidColorBrushx:Key="BaseSolidColorBrush"Color="{StaticResource BaseColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="BaseMoveColor"po:Freeze="True">#F5F7FA </Color>
<SolidColorBrushx:Key="BaseMoveColorSolidColorBrush"Color="{StaticResource BaseMoveColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="LighterColor"po:Freeze="True">#EBEEF5 </Color>
<SolidColorBrushx:Key="LighterSolidColorBrush"Color="{StaticResource LighterColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="LightColor"po:Freeze="True">#E4E7ED </Color>
<SolidColorBrushx:Key="LightSolidColorBrush"Color="{StaticResource LightColor}"po:Freeze="True"></SolidColorBrush>
</ResourceDictionary>
新建黑夜资源文件 Dark.Color.xaml;
<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"po:Freeze="True">
<!--字体颜色-->
<Colorx:Key="PrimaryTextColor"po:Freeze="True">#FFFFFF </Color>
<SolidColorBrushx:Key="PrimaryTextSolidColorBrush"Color="{StaticResource PrimaryTextColor}"po:Freeze="True"></SolidColorBrush>
<SolidColorBrushx:Key="WindowForegroundColorBrush"Color="{StaticResource PrimaryTextColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="RegularTextColor"po:Freeze="True">#FFFFFF </Color>
<SolidColorBrushx:Key="RegularTextSolidColorBrush"Color="{StaticResource RegularTextColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="DefaultBackgroundColor"po:Freeze="True">#202020 </Color>
<SolidColorBrushx:Key="DefaultBackgroundSolidColorBrush"Color="{StaticResource DefaultBackgroundColor}"po:Freeze="True"></SolidColorBrush>
<!--背景色-->
<Colorx:Key="BackgroundColor"po:Freeze="True">#323232 </Color>
<SolidColorBrushx:Key="BackgroundSolidColorBrush"Color="{StaticResource BackgroundColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="WindowBorderBrushColor"po:Freeze="True">#202020 </Color>
<SolidColorBrushx:Key="WindowBorderBrushSolidColorBrush"Color="{StaticResource WindowBorderBrushColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="BaseColor"po:Freeze="True">#202020 </Color>
<SolidColorBrushx:Key="BaseSolidColorBrush"Color="{StaticResource BaseColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="BaseMoveColor"po:Freeze="True">#202020 </Color>
<SolidColorBrushx:Key="BaseMoveColorSolidColorBrush"Color="{StaticResource BaseMoveColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="LighterColor"po:Freeze="True">#202020 </Color>
<SolidColorBrushx:Key="LighterSolidColorBrush"Color="{StaticResource LighterColor}"po:Freeze="True"></SolidColorBrush>
<Colorx:Key="LightColor"po:Freeze="True">#202020 </Color>
<SolidColorBrushx:Key="LightSolidColorBrush"Color="{StaticResource LightColor}"po:Freeze="True"></SolidColorBrush>
</ResourceDictionary>
新建 Resources继承自 ResourceDictionary实现加载黑夜或白天快手极速版下载安装2022最新版的模式;
usingSystem;
usingSystem.Windows;
usingWPFDevelopers.Minimal.Helpers;
namespaceWPFDevelopers.Minimal
{
publicclassResources: ResourceDictionary
{
publicThemeType Theme
{
set=> InitializeTheme( value);
}
protectedvoidInitializeTheme(ThemeType themeType)
{
MergedDictionaries.Clear;
varpath = GetResourceUri(GetThemeResourceName(themeType));
MergedDictionaries.Add( newResourceDictionary { Source = path });
}
protectedUri GetResourceUri(stringpath)
{
returnnewUri( $"pack://application:,,,/WPFDevelopers.Minimal;component/Themes/Basic/{path}.xaml");
}
protectedstringGetThemeResourceName(ThemeType themeType)
{
returnthemeType == ThemeType.Light ? "Light.Color": "Dark.Color";
}
}
}
使用只需要在项目快手极速版下载安装2022最新版的 App.Xaml添加命名空间 xmlns:ws="https://github.com/WPFDevelopersOrg.WPFDevelopers.Minimal" 然后在字典资源中添加
<!--需要注意ws:Resources 必须再配色主题后快手极速版下载安装2022最新版,Theme="Dark" 黑皮肤|Theme="Light" 白皮肤 -->
<ws:ResourcesTheme="Light"/>
动态切换需要修改 App.Xaml中的字典项的 ws:Resources 的 Theme 的值;
publicstaticvoidToggleLightAndDark(boolisDark = false)
{
vartype = isDark ? ThemeType.Dark : ThemeType.Light;
varexistingResourceDictionary =
Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x isResources) asResources;
if(existingResourceDictionary != null)
{
existingResourceDictionary.Theme = type;
if(type == ThemeType.Light)
{
varvBrush = Application.Current.Resources[ "PrimaryNormalSolidColorBrush"] asBrush;
Application.Current.Resources[ "WindowBorderBrushSolidColorBrush"] = vBrush;
WindowForegroundBrush = Application.Current.Resources[ "PrimaryTextSolidColorBrush"] asBrush;
if(Application.Current.Resources[ "DefaultBackgroundColor"] isColor color)
Application.Current.Resources[ "DefaultBackgroundSolidColorBrush"] = newSolidColorBrush(color);
}
else
{
if(Application.Current.Resources[ "WindowBorderBrushColor"] isColor color)
{
varcolorBrush = newSolidColorBrush(color);
Application.Current.Resources[ "WindowBorderBrushSolidColorBrush"] = colorBrush;
Application.Current.Resources[ "DefaultBackgroundSolidColorBrush"] = colorBrush;
}
WindowForegroundBrush = Application.Current.Resources[ "DefaultBackgroundSolidColorBrush"] asBrush;
}
Brush = Application.Current.Resources[ "BackgroundSolidColorBrush"] asBrush;
//WindowForegroundBrush = Application.Current.Resources["PrimaryTextSolidColorBrush"] as Brush;
_IsCurrentDark = isDark;
ThemeRefresh;
}
}
publicstaticvoidThemeRefresh()
{
varthemePath = "pack://application:,,,/WPFDevelopers.Minimal;component/Themes/Theme.xaml";
varthemeResourceDictionary =
Application.Current.Resources.MergedDictionaries.FirstOrDefault(x =>
x.Source != null&& x.Source.Equals(themePath));
if(themeResourceDictionary == null) return;
Application.Current.Resources.MergedDictionaries.Remove(themeResourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(themeResourceDictionary);
OnSubThemeChanged;
}
切换调用如下;
privatevoidLightDark_Checked(objectsender, RoutedEventArgs e)
{
varlightDark = sender asToggleButton;
if(lightDark == null) return;
ControlHelper.ToggleLightAndDark(lightDark.IsChecked == true);
}
源码GitHub[1]源码Gitee[2]
其他基础控件
1.Window
2.Button
3.CheckBox
4.ComboBox
5.DataGrid
6.DatePicker
7.Expander
8.GroupBox
9.ListBox
10.ListView
11.Menu
12.PasswordBox
13.TextBox
14.RadioButton
15.ToggleButton
16.Slider
17.TreeView
18.TabControl
参考资料
[1]
GitHub: https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal
[2]
Gitee: https://gitee.com/WPFDevelopersOrg/WPFDevelopers.Minimal
标签: 快手极速版下载安装2022最新版