简体   繁体   English

WPF 如何给多个资源字典相同的 class?

[英]WPF How to give multiple resource dictionaries the same class?

I'm not sure if I'm asking the right type of question, but here's my issue.我不确定我是否在问正确类型的问题,但这是我的问题。

I have multiple resource dictionaries, and I would like to allow for users to customise these resource dictionaries to create their own styles.我有多个资源字典,我想允许用户自定义这些资源字典来创建自己的 styles。 There are styles built in , and styles loaded from files, each style has a single resource dictionary with a specified class .内置的 styles和从文件加载的 styles ,每种样式都有一个带有指定 class的资源字典。

I wanted to keep the styles separate so they could be customised, but I need some event handlers, so I made a generic event handler class ResourceDictionaryEventHandler.cs .我想将 styles 分开,以便对其进行自定义,但我需要一些事件处理程序,所以我制作了一个通用事件处理程序 class ResourceDictionaryEventHandler.cs

The issue now is when I assign more than one resource dictionary the same class, I get this error .现在的问题是,当我为多个资源字典分配相同的 class 时,我收到此错误 The app.xaml had some of the styles already loaded, but even after only loading one set of styles I still got the same error. app.xaml已经加载了一些 styles,但即使只加载了一组 styles 我仍然得到同样的错误。

Its weird too because it seemed to be working earlier, but simply restarting Visual Studio the issue surfaced.它也很奇怪,因为它似乎更早地工作,但只需重新启动 Visual Studio,问题就浮出水面。

This image does have 2 resource dictionaries per style, but only one have them has been assigned a class.此图像的每种样式确实有 2 个资源字典,但只有一个资源字典被分配了 class。

So how do I assign this EventHandler class to multiple resource dictionaries?那么如何将这个 EventHandler class 分配给多个资源字典呢? Or is there a better way of doing it?或者有更好的方法吗?

I made a small example for you.我为你做了一个小例子。 I have added two resource dictionaries named Dark.xaml and Light.xaml to the project.我在项目中添加了两个名为 Dark.xaml 和 Light.xaml 的资源字典。

在此处输入图像描述

I have defined the colors that I will use in these resource dictionaries.我已经定义了将在这些资源字典中使用的 colors。

Dark.xaml Code 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 code灯号.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>

Then I added two buttons to the home page and changed the source dictionaries to change the color of the frame.然后我在主页上添加了两个按钮并更改了源字典以更改框架的颜色。

MainWindow.xaml Code 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 Code 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 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);
    }

Don't forget to include the same color key in app.xaml.不要忘记在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM