WPF如何给多个资源字典同一个类?

时间:2021-01-24 19:47:30

标签: c# wpf visual-studio xaml resourcedictionary

我不确定我问的问题类型是否正确,但这是我的问题。

我有多个资源词典,我希望允许用户自定义这些资源词典以创建自己的样式。有 styles built in 和从文件加载的样式,每个样式都有一个带有 a specified class 的资源字典。

我想将样式分开以便可以自定义,但我需要一些事件处理程序,所以我创建了一个通用的事件处理程序类 ResourceDictionaryEventHandler.cs

现在的问题是,当我将多个资源字典分配给同一个类时,我得到 this errorapp.xaml 已经加载了一些样式,但即使只加载了一组样式后,我仍然遇到相同的错误。

它也很奇怪,因为它似乎可以更早地工作,但只需重新启动 Visual Studio,问题就会浮出水面。

This image 每个样式确实有 2 个资源词典,但只有一个为它们分配了一个类。

那么如何将这个 EventHandler 类分配给多个资源字典呢?或者有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

我为你做了一个小例子。我在项目中添加了两个名为 Dark.xaml 和 Light.xaml 的资源词典。

enter image description here

我已经定义了我将在这些资源词典中使用的颜色。

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_点击代码

 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);
    }

暗按钮_点击

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>
相关问题