WPF 动态切换黑|白皮肤

 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;

<ResourceDictionary xmlns="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"><!--字体颜色--><Color x:Key="PrimaryTextColor" po:Freeze="True">#303133</Color><SolidColorBrush x:Key="PrimaryTextSolidColorBrush" Color="{StaticResource PrimaryTextColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="RegularTextColor" po:Freeze="True">#606266</Color><SolidColorBrush x:Key="RegularTextSolidColorBrush" Color="{StaticResource RegularTextColor}" po:Freeze="True"></SolidColorBrush><!--背景色--><Color x:Key="BackgroundColor" po:Freeze="True">#FFFFFF</Color><SolidColorBrush x:Key="BackgroundSolidColorBrush" Color="{StaticResource BackgroundColor}" po:Freeze="True"></SolidColorBrush><SolidColorBrush x:Key="WindowForegroundColorBrush" Color="{StaticResource BackgroundColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="BaseColor" po:Freeze="True">#DCDFE6</Color><SolidColorBrush x:Key="BaseSolidColorBrush" Color="{StaticResource BaseColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="BaseMoveColor" po:Freeze="True">#F5F7FA</Color><SolidColorBrush x:Key="BaseMoveColorSolidColorBrush" Color="{StaticResource BaseMoveColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="LighterColor" po:Freeze="True">#EBEEF5</Color><SolidColorBrush x:Key="LighterSolidColorBrush" Color="{StaticResource LighterColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="LightColor" po:Freeze="True">#E4E7ED</Color><SolidColorBrush x:Key="LightSolidColorBrush" Color="{StaticResource LightColor}" po:Freeze="True"></SolidColorBrush></ResourceDictionary>
  • 新建黑夜资源文件 Dark.Color.xaml;

<ResourceDictionary xmlns="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"><!--字体颜色--><Color x:Key="PrimaryTextColor" po:Freeze="True">#FFFFFF</Color><SolidColorBrush x:Key="PrimaryTextSolidColorBrush" Color="{StaticResource PrimaryTextColor}" po:Freeze="True"></SolidColorBrush><SolidColorBrush x:Key="WindowForegroundColorBrush" Color="{StaticResource PrimaryTextColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="RegularTextColor" po:Freeze="True">#FFFFFF</Color><SolidColorBrush x:Key="RegularTextSolidColorBrush" Color="{StaticResource RegularTextColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="DefaultBackgroundColor" po:Freeze="True">#202020</Color><SolidColorBrush x:Key="DefaultBackgroundSolidColorBrush" Color="{StaticResource DefaultBackgroundColor}" po:Freeze="True"></SolidColorBrush><!--背景色--><Color x:Key="BackgroundColor" po:Freeze="True">#323232</Color><SolidColorBrush x:Key="BackgroundSolidColorBrush" Color="{StaticResource BackgroundColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="WindowBorderBrushColor" po:Freeze="True">#202020</Color><SolidColorBrush x:Key="WindowBorderBrushSolidColorBrush" Color="{StaticResource WindowBorderBrushColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="BaseColor" po:Freeze="True">#202020</Color><SolidColorBrush x:Key="BaseSolidColorBrush" Color="{StaticResource BaseColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="BaseMoveColor" po:Freeze="True">#202020</Color><SolidColorBrush x:Key="BaseMoveColorSolidColorBrush" Color="{StaticResource BaseMoveColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="LighterColor" po:Freeze="True">#202020</Color><SolidColorBrush x:Key="LighterSolidColorBrush" Color="{StaticResource LighterColor}" po:Freeze="True"></SolidColorBrush><Color x:Key="LightColor" po:Freeze="True">#202020</Color><SolidColorBrush x:Key="LightSolidColorBrush" Color="{StaticResource LightColor}" po:Freeze="True"></SolidColorBrush></ResourceDictionary>
  • 新建Resources继承自ResourceDictionary实现加载黑夜或白天的模式;

using System;
using System.Windows;
using WPFDevelopers.Minimal.Helpers;namespace WPFDevelopers.Minimal
{public class Resources : ResourceDictionary{public ThemeType Theme{set => InitializeTheme(value);}protected void InitializeTheme(ThemeType themeType){MergedDictionaries.Clear();var path = GetResourceUri(GetThemeResourceName(themeType));MergedDictionaries.Add(new ResourceDictionary { Source = path });}protected Uri GetResourceUri(string path){return new Uri($"pack://application:,,,/WPFDevelopers.Minimal;component/Themes/Basic/{path}.xaml");}protected string GetThemeResourceName(ThemeType themeType){return themeType == ThemeType.Light ? "Light.Color" : "Dark.Color";}}
}
  • 使用只需要在项目的 App.Xaml 添加命名空间 xmlns:ws="https://github.com/WPFDevelopersOrg.WPFDevelopers.Minimal" 然后在字典资源中添加

<!--需要注意ws:Resources 必须再配色主题后,Theme="Dark" 黑皮肤|Theme="Light" 白皮肤 -->
<ws:Resources Theme="Light"/>
1e36b90c76d015d5aa5fb96f77a96105.pnga7f6b84413ea3c3712d68e568a315f7b.png
  • 动态切换需要修改 App.Xaml 中的字典项的 ws:ResourcesTheme 的值;

public static void ToggleLightAndDark(bool isDark = false){var type = isDark ? ThemeType.Dark : ThemeType.Light;var existingResourceDictionary =Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x is Resources) as Resources;if (existingResourceDictionary != null){existingResourceDictionary.Theme = type;if (type == ThemeType.Light){var vBrush = Application.Current.Resources["PrimaryNormalSolidColorBrush"] as Brush;Application.Current.Resources["WindowBorderBrushSolidColorBrush"] = vBrush;WindowForegroundBrush = Application.Current.Resources["PrimaryTextSolidColorBrush"] as Brush;if (Application.Current.Resources["DefaultBackgroundColor"] is Color color)Application.Current.Resources["DefaultBackgroundSolidColorBrush"] = new SolidColorBrush(color);}else{if (Application.Current.Resources["WindowBorderBrushColor"] is Color color){var colorBrush = new SolidColorBrush(color);Application.Current.Resources["WindowBorderBrushSolidColorBrush"] = colorBrush;Application.Current.Resources["DefaultBackgroundSolidColorBrush"] = colorBrush;}WindowForegroundBrush = Application.Current.Resources["DefaultBackgroundSolidColorBrush"] as Brush;}Brush = Application.Current.Resources["BackgroundSolidColorBrush"] as Brush;//WindowForegroundBrush = Application.Current.Resources["PrimaryTextSolidColorBrush"] as Brush;_IsCurrentDark = isDark;ThemeRefresh();}}public static void ThemeRefresh(){var themePath = "pack://application:,,,/WPFDevelopers.Minimal;component/Themes/Theme.xaml";var themeResourceDictionary =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();}
  • 切换调用如下;

private void LightDark_Checked(object sender, RoutedEventArgs e)
{var lightDark = sender as ToggleButton;if (lightDark == null) return;ControlHelper.ToggleLightAndDark(lightDark.IsChecked == true);
}
f90635fac08c5145a7e2f36f0ddd2929.gif

源码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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/283651.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

中小企业虚拟化解决方案-VMware vSphere 6.5-日常管理入口v0.0.1

中小企业虚拟化解决方案-VMware vSphere 6.5日常管理入口v0.0.1本文目的&#xff1a;针对中小企业虚拟化的平台管理&#xff0c;涉及到很多管理入口&#xff0c;普通管理员未必知道从哪里管理?本文将从最底层到最高层进行简单的介绍&#xff0c;最终让普通管理员快速了解管理入…

Svn服务器的搭建与配置

本文由ilanniweb提供友情赞助&#xff0c;首发于烂泥行天下想要获得更多的文章&#xff0c;可以关注我的微信ilanniweb要把svn代码同步到git服务器上&#xff0c;本来是想通过subgit直接同步进行就行了。但是自已以前没有搭建过svn服务器&#xff0c;所以有了这篇文章。我们就来…

JAVA Future类详解

1. Future的应用场景 在并发编程中&#xff0c;我们经常用到非阻塞的模型&#xff0c;在之前的多线程的三种实现中&#xff0c;不管是继承thread类还是实现runnable接口&#xff0c;都无法保证获取到之前的执行结果。通过实现Callback接口&#xff0c;并用Future可以来接收多线…

最新 .NET 社区工具包, 推出MVVM 源代码生成器!

点击上方蓝字关注我们&#xff08;本文阅读时间&#xff1a;10分钟)我们很高兴地宣布正式推出新的 .NET 社区工具包&#xff0c;现在已经在NuGet上发布了8.0.0版本&#xff01;这是一个重要版本&#xff0c;包括大量新功能、改进、优化、错误修复&#xff0c;许多反映了全新项目…

【bzoj4145】[AMPPZ2014]The Prices 状压dp

原文地址&#xff1a;http://www.cnblogs.com/GXZlegend/p/6832200.html 题目描述 你要购买m种物品各一件&#xff0c;一共有n家商店&#xff0c;你到第i家商店的路费为d[i]&#xff0c;在第i家商店购买第j种物品的费用为c[i][j]&#xff0c;求最小总费用。输入 第一行包含两个…

java总结

1.org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.supermap.model.meta.META_DS 保存或更新实体类的对象&#xff0c;需设置ID属性。否则会报上述错误。 setId(); 2.定义泛型方法 定义泛型方法时&…

Java并发编程:Executor、Executors、ExecutorService

Executors 在Java 5之后&#xff0c;并发编程引入了一堆新的启动、调度和管理线程的API。Executor框架便是Java 5中引入的&#xff0c;其内部使用了线程池机制&#xff0c;它在java.util.cocurrent 包下&#xff0c;通过该框架来控制线程的启动、执行和关闭&#xff0c;可以简化…

IOTCS+Ekuiper搭建物联网边缘计算平台

背景介绍IOTCS 是专为物联网平台而设计的工业智能网关。自从 2020 年 10 月以来&#xff0c;我们从需求调研&#xff0c;设计&#xff0c;定型&#xff0c;研发&#xff0c;测试经过漫长的沉淀与孵化&#xff0c;最终顺利实现工业智能网关最初的设想。我们凭借创新设计理念、快…

监听键盘弹出高度

- (void)registerForKeyboardNotifications {//使用NSNotificationCenter 鍵盤出現時[[NSNotificationCenter defaultCenter] addObserver:selfselector:selector(keyboardWillShown:)name:UIKeyboardWillChangeFrameNotification object:nil];//使用NSNotificationCenter 鍵盤…

[NOIP1999] 提高组 洛谷P1014 Cantor表

题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的。他是用下面这一张表来证明这一命题的&#xff1a; 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … 3/1 3/2 3/3 … 4/1 4/2 … 5/1 … … 我们以Z字形给上表的每一项编号。第一项是1/1&#xff0c;然后是1/…

JMX 使用指南一 Java Management Extensions

1. 什么是 JMX JMX&#xff0c;全称 Java Management Extensions&#xff0c;是在 J2SE 5.0 版本中引入的一个功能。提供了一种在运行时动态管理资源的框架&#xff0c;主要用于企业应用程序中实现可配置或动态获取应用程序的状态。JMX 提供了一种简单、标准的监控和管理资源的…

WinForm(七)在新线程中更新UI

在WinForm项目中&#xff0c;很多时候会映遇上多线程一起工作的情况&#xff0c;因为当前UI的更新显示&#xff0c;是在主线程中&#xff0c;一但主线程被长时运算占据后&#xff0c;UI就会被卡信&#xff0c;出现假死现像。那么就需要起一个新线程做长时运算工作&#xff0c;把…

多种方法实现自适应布局

最近切了几个手机端的网页&#xff0c;第一次切的是美团的首页&#xff0c;为了自适应不同的手机分辨率&#xff0c;需要用到自适应布局&#xff0c;切图的时候是用的第一中方法&#xff0c;用到了定位&#xff0c;后来查找了一些其他方法&#xff0c;现在就介绍几种自适应布局…

hivesql优化的深入解析

转载&#xff1a;https://www.csdn.net/article/2015-01-13/2823530 一个Hive查询生成多个Map Reduce Job&#xff0c;一个Map Reduce Job又有Map&#xff0c;Reduce&#xff0c;Spill&#xff0c;Shuffle&#xff0c;Sort等多个阶段&#xff0c;所以针对Hive查询的优化可以大致…

如何用一行 CSS 实现 10 种现代布局

现代 CSS 布局使开发人员只需按几下键就可以编写十分有意义且强大的样式规则。上面的讨论和接下来的帖文研究了 10 种强大的 CSS 布局&#xff0c;它们实现了一些非凡的工作。 01. 超级居中&#xff1a;place-items: center 对于第一个“单行”布局&#xff0c;让我们解决所有 …

谈谈我对MVC的View层实现的理解

谈谈我对MVC的View层实现的理解 MVC框架可以把应用清晰明了地分为三个部分&#xff1a;Model层–数据层&#xff0c;View层–视图层&#xff0c;Controller–逻辑层&#xff0c;Model层负责整合数据&#xff0c;View层负责页面渲染&#xff0c;Controller层负责实现业务逻辑。 …

在.NET 6.0中使用不同的托管模型

本章是《定制ASP NET 6.0框架系列文章》的第六篇。在本章中&#xff0c;我们将讨论如何在ASP NET 6.0中自定义托管宿主。比如&#xff0c;托管选项和不同类型的托管&#xff0c;并了解一下IIS上的托管。限于篇幅&#xff0c;本章只是一个抛砖迎玉。本章涵盖主题包括&#xff1a…

Supplemental Logging

Supplemental Logging分为两种&#xff1a;Database-Level Supplemental Logging和Table-Level Supplemental Logging&#xff0c;即数据库级别和表级别。下面我们来看看Oracle官方文档对其的介绍和说明&#xff0c;引自 http://docs.oracle.com/cd/E11882_01/server.112/e2249…

TypeScript 与 JavaScript 的区别

TypeScript 是 JavaScript 的一个超集&#xff0c;支持 ECMAScript 6 标准&#xff08;ES6 教程&#xff09;。TypeScript 由微软开发的自由和开源的编程语言。TypeScript 设计目标是开发大型应用&#xff0c;它可以编译成纯 JavaScript&#xff0c;编译出来的 JavaScript 可以…

【caffe】create_cifar10.sh在windows下解决方案

tags caffe python windows下配置caffe后&#xff0c;create_cifar10.sh无法执行&#xff0c;因为是shell脚本。那就看懂脚本意思&#xff0c;用python重写一个&#xff1a; # create_cifar10.py # by ChrisZZimport os import shutilEXAMPLE"examples\\cifar10" DAT…