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"/>

  • 动态切换需要修改 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);
}

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

WPF 动态切换黑|白皮肤相关推荐

  1. WPF 动态切换按钮图片

    WPF动态切换按钮图片就是在鼠标移上去的时候显示另一张图片 首先先把三张图片放上去 第一张 第二张 第三张 然后给他们一个值,鼠标移上去的时候是true,显示图片,鼠标移开的时候显示false不显示图 ...

  2. aswing学习笔记4-通过调用面板中的按钮实现主界面动态切换皮肤的问题!

    通过调用面板中的按钮实现主界面动态切换皮肤的问题! 发表于 : 周三 10月 29, 2008 2:09 pm 由 xueyuan cyz 现在我在做一个动态切换皮肤的的功能,原理是通过点击 调用面板 ...

  3. WPF案例 (六) 动态切换UI布局

    原文:WPF案例 (六) 动态切换UI布局 这个Wpf示例对同一个界面支持以ListView或者CardView的布局方式呈现界面,使用控件ItemsControl绑定数据源,使用DataTempla ...

  4. element如何动态切换主题(vite+vue+ts+elementPlus)

    前言 提示:动态切换主题使用的是css3的var函数现实 示例:切换--main-bg-color的值,使用<div style="--main-bg-color:red"& ...

  5. springboot+mybatis实现动态切换数据源

    目前有个需求,需要使用不同的数据源,例如某业务要用A数据源,另一个业务要用B数据源. 如何在spring框架中解决多数据源的问题 使用springboot 整合多数据源 遇到的坑 1.添加依赖 < ...

  6. Android动态切换主题

    软件换肤从功能上可以划分三种: 1) 软件内置多个皮肤,不可由用户增加或修改: 最低的自由度,软件实现相对于后两种最容易. 2) 官方提供皮肤供下载,用户可以使用下载的皮肤: 用户可选择下载自己喜欢的 ...

  7. 手把手教你玩多数据源动态切换

    为了提高应用的可靠性,多数据源现在也很常见,数据库可以搭建双 M 结构,这个松哥之前也发文和大家分享过如何搭建双 M 结构的主从备份?,那么 Java 代码里该如何操作多数据源呢? 我在 19 年的时 ...

  8. Android插件化开发之动态加载本地皮肤包进行换肤

    Android插件化开发之动态加载本地皮肤包进行换肤 前言: 本文主要讲解如何用开源换肤框架 android-skin-loader-lib来实现加载本地皮肤包文件进行换肤,具体可自行参考框架原理进行 ...

  9. spring boot 动态切换数据源实现多租户开发

    之前的文章有介绍过spring boot 动态切换数据源spring boot 动态切换数据源(数据源信息从数据库中读取)_lgq2016的博客-CSDN博客,今天简单介绍一下动态数据源切换实战,主要 ...

最新文章

  1. ISP【三】———— raw读取、不同格式图片差异
  2. java中使用json以及所导入的包
  3. 【研究院】滴滴研究院,都在做什么
  4. 云炬随笔20211002
  5. php的验证码要gd库,怎么在PHP中使用GD库实现一个验证码功能
  6. note2 android4.3,玩家们动手吧 Note2安卓4.3固件已泄漏
  7. db2查最新值的前一天值_贵阳6月最新二手房房价出炉!快看你家房子值多少钱?...
  8. 程序员进入BAT,到底是“好事”还是“坏事”?
  9. ubuntu+火狐浏览器+印象笔记+剪藏+国内版配置
  10. 计算机成绩统计优秀率,高校学生考试成绩的数据分析模式与可视化研究
  11. 关于“八音盒自定义弹奏”的一些想法
  12. 如何u盘装系统win10 64位?
  13. 深入理解多线程(四)—— Moniter的实现原理
  14. python重试组件tenacity介绍
  15. 债务人无力偿还,债权人可否直接起诉“次债务人”
  16. 2020年日历_2020年农历阳历表,2020年日历表,2020年黄历
  17. 服务器虚拟化技术主要有什么优势
  18. PicPick Pro v7.0.0 屏幕截图编辑工具解锁全功能单文件版
  19. ajax success 参数
  20. Python判断变量的类型

热门文章

  1. NC63发送消息实例
  2. Jmeter生成本地web测试报告遇到的问题及解决方式
  3. 你知道什么是光刻胶吗
  4. wxpython手册_wxpython中文手册
  5. 一张图看明白部标808协议
  6. 电子元器件—POGO PIN连接器
  7. 2018年1月学习心得报告
  8. 最高院《关于修改民诉证据的决定》施行!一文get可靠电子数据存证
  9. 《记忆》———李培根昨日演讲稿摘录[全文]
  10. 计算机科学与技术和数学与应用数学 女生,女生适合报考的15个专业 哪些专业前景好...