简体   繁体   English

WPF使用来自多个项目的多个资源字典

[英]WPF Using multiple Resource Dictionaries from multiple projects

I have two class Library projects: Project A.Themes Project B.Themes 我有两个班级的图书馆项目:项目A.Themes项目B.Themes

Project A.Themes is my base Themes Project. 项目A.Themes是我的基本主题项目。 Project B.Themes using A.Themes and have new styles and some of the resources have keys that already defined in A.Themes. 使用A.Themes的项目B.Themes具有新样式,并且某些资源具有A.Themes中已定义的键。

We want to use this two themes in our Project, and if we use a resource that is defined in both of the project we want to take the resource from B.Themes. 我们想在我们的项目中使用这两个主题,如果我们使用在两个项目中都定义的资源,那么我们想从B.Themes中获取资源。

This is our code: 这是我们的代码:

A.Themes have few files of styles: A.Themes有很少的样式文件:

Brushes.xaml
Buttons.xaml
CheckBox.xaml

etc.. 等等..

we load them in Bundle.Xaml: 我们将它们加载到Bundle.Xaml中:

<ResourceDictionary.MergedDictionaries>         
   <ResourceDictionary Source="pack://application:,,,/A.Themes;component/Assets/Brushes.xaml"/>
   <ResourceDictionary Source="pack://application:,,,/A.Themes;component/Assets/Buttons.xaml"/>
   <ResourceDictionary Source="pack://application:,,,/A.Themes;component/Assets/CheckBox.xaml" />
</ResourceDictionary.MergedDictionaries>

B.Themes have the same Files: B.Themes具有相同的文件:

Brushes.xaml
Buttons.xaml
CheckBox.xaml

we load them in Bundle.Xaml and adding the bundle of A.Themes: 我们将它们加载到Bundle.Xaml中,并添加A.Themes捆绑包:

<ResourceDictionary.MergedDictionaries>         
   <ResourceDictionary Source="pack://application:,,,/A.Themes;component/Bundle.xaml"/>
   <ResourceDictionary Source="pack://application:,,,/B.Themes;component/Assets/Brushes.xaml"/>
   <ResourceDictionary Source="pack://application:,,,/B.Themes;component/Assets/Buttons.xaml"/>
   <ResourceDictionary Source="pack://application:,,,/B.Themes;component/Assets/CheckBox.xaml" />
</ResourceDictionary.MergedDictionaries>

In our Project we load them in App.xaml: 在我们的项目中,我们将它们加载到App.xaml中:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/A.Themes;component/Bundle.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/B.Themes;component/Bundle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

the problems are: 1. it not always takes the resources from B.Themes and we can't find out why. 问题是:1.它并不总是从B.Themes那里获取资源,我们找不到原因。 2. if i remove the reference to A.Themes/Bundle.xaml from app.xaml the project can't find resources from A.Themes even though it's included in B.Themes/Bundle.xaml 2.如果我从app.xaml中删除了对A.Themes / Bundle.xaml的引用,即使该项目包含在B.Themes / Bundle.xaml中,该项目也无法从A.Themes中找到资源。

note: we have refernce to A.Themes project in B.Themes and refernce to A.Themes and B.Themes in main project 注意:我们在B.Themes中引用了A.Themes项目,并且在主项目中引用了A.Themes和B.Themes。

can someone please help me to understande what is going on here? 有人可以帮我了解一下这里的事吗? thanks! 谢谢!

The loading order isn't quite what you expect. 加载顺序与您期望的不太一样。 From MSDN: 从MSDN:

Resources in a merged dictionary occupy a location in the resource lookup scope that is just after the scope of the main resource dictionary they are merged into 合并字典中的资源在资源查找范围中占据一个位置,该位置恰好在将它们合并到主资源字典的范围之后

https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/merged-resource-dictionaries https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/merged-resource-dictionaries

So the dictionaries that merge into Bundle.xaml of assembly A are actually loaded after the other ones. 因此,合并到程序集A的Bundle.xaml中的字典实际上是在其他字典之后加载的。

Please refer to the following link for more information and an example of the same behaviour: https://social.msdn.microsoft.com/Forums/vstudio/en-US/3bea80f9-d1db-4cb7-ae7a-77a02eaf4ec9/resourcedictionary-load-order?forum=wpf 请参考以下链接以获取更多信息和相同行为的示例: https : //social.msdn.microsoft.com/Forums/vstudio/en-US/3bea80f9-d1db-4cb7-ae7a-77a02eaf4ec9/resourcedictionary-load阶?论坛= WPF

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

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