0

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

我有多个资源字典,我希望允许用户自定义这些资源字典以创建自己的样式。有内置的样式,以及从文件加载的样式,每种样式都有一个带有指定类的资源字典。

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

现在的问题是,当我为同一个类分配多个资源字典时,我会收到此错误。app.xaml已经加载了一些样式,但即使只加载了一组样式,我仍然遇到同样的错误

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

此图像的每种样式确实有 2 个资源字典,但只有一个资源字典已被分配一个类。

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

4

1 回答 1

0

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

在此处输入图像描述

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

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>
于 2021-01-25T08:02:11.787 回答