当前位置: 首页 > news >正文

样式资源-切换主题,动态切换字典文件

这个例子说明,多个字典文件中引用同一个资源key,可以切换文件使用不同的资源字典。

1.资源key类

using System.Windows;
namespace 动态资源_主题切换
{public static class ResourceKeys{// 前景色资源键(用于文本颜色)public static readonly ResourceKey TextForeground = new ComponentResourceKey(typeof(ResourceKeys), "TextForeground");// 按钮背景资源键public static readonly ResourceKey ButtonBackground = new ComponentResourceKey(typeof(ResourceKeys), "ButtonBackground");// 窗口背景资源键public static readonly ResourceKey WindowBackground = new ComponentResourceKey(typeof(ResourceKeys), "WindowBackground");}
}

2.暗色主题的字典和亮色主题的字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:动态资源_主题切换"><!-- 对应ResourceKeys.TextForeground的资源值 --><SolidColorBrush x:Key="{x:Static local:ResourceKeys.TextForeground}" Color="White" /><!-- 对应ResourceKeys.ButtonBackground的资源值 --><SolidColorBrush x:Key="{x:Static local:ResourceKeys.ButtonBackground}" Color="#2E7D32" /><!-- 深绿 --><!-- 对应ResourceKeys.WindowBackground的资源值 --><SolidColorBrush x:Key="{x:Static local:ResourceKeys.WindowBackground}" Color="#333333" /><!-- 深灰 -->
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:动态资源_主题切换"><!-- 对应ResourceKeys.TextForeground的资源值 --><SolidColorBrush x:Key="{x:Static local:ResourceKeys.TextForeground}" Color="Black" /><!-- 对应ResourceKeys.ButtonBackground的资源值 --><SolidColorBrush x:Key="{x:Static local:ResourceKeys.ButtonBackground}" Color="LightBlue" /><!-- 绿色 --><!-- 对应ResourceKeys.WindowBackground的资源值 --><SolidColorBrush x:Key="{x:Static local:ResourceKeys.WindowBackground}" Color="White" />
</ResourceDictionary>

3.合并字典,但迷人和不亮色的

<Application x:Class="动态资源_主题切换.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:动态资源_主题切换"StartupUri="MainWindow.xaml"><Application.Resources><!-- 合并默认主题(浅色主题) --><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="LightTheme.xaml" /></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
</Application>

4.使用字典,并且可以切换

<Window x:Class="动态资源_主题切换.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:动态资源_主题切换"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"Background="{DynamicResource {x:Static local:ResourceKeys.WindowBackground}}"><Window.Resources><Style TargetType="Button" x:Key="ThemeButtonStyle"><!-- 绑定按钮背景到资源键(动态更新) --><Setter Property="Background" Value="{DynamicResource {x:Static local:ResourceKeys.ButtonBackground}}" /><!-- 绑定按钮前景色到资源键(动态更新) --><Setter Property="Foreground" Value="{DynamicResource {x:Static local:ResourceKeys.TextForeground}}" /><Setter Property="Padding" Value="10,5" /><Setter Property="Margin" Value="5" /></Style></Window.Resources><StackPanel Margin="20" HorizontalAlignment="Center"><!-- 文本前景色绑定到资源键 --><TextBlock Text="这是一段测试文本" Foreground="{DynamicResource {x:Static local:ResourceKeys.TextForeground}}"FontSize="16" Margin="5" /><!-- 使用自定义样式的按钮 --><Button Content="普通按钮" Style="{StaticResource ThemeButtonStyle}" /><!-- 切换主题的按钮 --><Button Content="切换主题" Click="SwitchTheme_Click" Style="{StaticResource ThemeButtonStyle}" /></StackPanel>
</Window>

后台代码的切换逻辑,是清楚合并的字典,添加另一个样式的字典

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace 动态资源_主题切换
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private bool _isDarkTheme = false; // 标记当前是否为深色主题private void SwitchTheme_Click(object sender, RoutedEventArgs e){// 清除当前应用合并的资源字典
            Application.Current.Resources.MergedDictionaries.Clear();// 根据当前主题切换资源if (_isDarkTheme){// 切换到浅色主题Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary{Source = new Uri("LightTheme.xaml", UriKind.Relative)});}else{// 切换到深色主题Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary{Source = new Uri("DarkTheme.xaml", UriKind.Relative)});}_isDarkTheme = !_isDarkTheme; // 反转主题标记
        }}
}

 

http://www.jsqmd.com/news/26833/

相关文章:

  • 堆和栈的生命周期对于代码的影响
  • AI 智能体开发实战零基础自学手册:理论到云端部署实战指南
  • AC自动机(拓扑排序优化)
  • 详细介绍:Leetcode 3700. Number of ZigZag Arrays II
  • work 3
  • moji 辞书 注音分析
  • 实用指南:OSPF LSA Type 3(Summary LSA)概念及题目
  • .net解决分布式事务简单方案DotNetCore.CAP
  • 《Ai元人文》
  • sklearn 特征选择实战:用 RFE 找到最优特征组合
  • 老旧环境torch版本(0.4.1)环境配置总结
  • ✨《那个让我准时下班的神器,藏在这份编辑器测评里》
  • 代码大全阅读笔记3
  • Newton记录
  • 【备份】不知道什么时候写的IniReader.js
  • CSS尺寸、盒子模型、定位、浮动与布局(Flex/Grid)
  • 通过中国信通院SQL质量管理最高等级评测,天翼云TeleDB引领数据库管理新标准!
  • AtCoder Regular Contest 208 (Div. 2) 题解
  • 第三十篇
  • 代码大阅读笔记
  • 第2次软件基础作业
  • 第二次软件基础作业
  • vs2017安装qt插件及安装qt插件后的设置
  • 实用指南:从0死磕全栈之Next.js Server Actions 入门实战:在服务端安全执行逻辑,告别 API 路由!
  • KeyShot许可管理故障排除步骤
  • 各式各样的Attention - -一叶知秋
  • 重塑生产力:天翼云全球首发RaaS,开启“机器人即服务”商业时代!
  • Python自然语言处理(NLP)入门
  • 【计算机视觉】分水岭搭建医学诊断
  • mysql和java获取经纬度的距离的两种方式