我为你做了一个小例子。我在项目中添加了两个名为 Dark.xaml 和 Light.xaml 的资源字典。
![在此处输入图像描述](https://i.stack.imgur.com/nHKd0.png)
我已经定义了我将在这些资源字典中使用的颜色。
Dark.xaml 代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ThemeExample">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<SolidColorBrush x:Key="ColorMode" Color="DarkBlue"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
Light.xaml 代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ThemeExample">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<SolidColorBrush x:Key="ColorMode" Color="LightBlue"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
然后我在主页上添加了两个按钮并更改了源字典以更改框架的颜色。
MainWindow.xaml 代码
<Button x:Name="DarkButton" Content="Dark" HorizontalAlignment="Left" Margin="151,132,0,0" VerticalAlignment="Top" Width="75" Click="DarkButton_Click"/>
<Button x:Name="LightButton" Content="Light" HorizontalAlignment="Left" Margin="248,132,0,0" VerticalAlignment="Top" Width="75" Click="LightButton_Click"/>
<Rectangle Fill="{DynamicResource ColorMode}" Width="100" Height="100"></Rectangle>
LightButton_Click 代码
private void LightButton_Click(object sender, RoutedEventArgs e)
{
var resources = Application.Current.Resources.MergedDictionaries;
var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
.Where(rd => rd.Source != null)
.SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/((Light)|(Dark)))").Success);
var source = $"pack://application:,,,/Light.xaml";
var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };
Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);
}
DarkButton_Click
private void DarkButton_Click(object sender, RoutedEventArgs e)
{
var resources = Application.Current.Resources.MergedDictionaries;
var existingResourceDictionary = Application.Current.Resources.MergedDictionaries
.Where(rd => rd.Source != null)
.SingleOrDefault(rd => Regex.Match(rd.Source.OriginalString, @"(\/((Light)|(Dark)))").Success);
var source = $"pack://application:,,,/Dark.xaml";
var newResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };
Application.Current.Resources.MergedDictionaries.Remove(existingResourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);
}
不要忘记在 app.xaml 中包含相同的颜色键。
<Application x:Class="ThemeExample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ThemeExample"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<SolidColorBrush x:Key="ColorMode" Color="DarkBlue"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>