WPF 使用来自多个项目的多个资源字典 [英] WPF Using multiple Resource Dictionaries from multiple projects

查看:122
本文介绍了WPF 使用来自多个项目的多个资源字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类库项目:项目A.主题项目B.主题

I have two class Library projects: Project A.Themes Project B.Themes

Project A.Themes 是我的基础主题项目.项目 B.Themes 使用 A.Themes 并具有新样式,并且某些资源具有已在 A.Themes 中定义的键.

Project A.Themes is my base Themes Project. Project B.Themes using A.Themes and have new styles and some of the resources have keys that already defined in A.Themes.

我们想在我们的项目中使用这两个主题,如果我们使用在两个项目中定义的资源,我们希望从 B.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.

这是我们的代码:

A.Themes 的样式文件很少:

A.Themes have few files of styles:

Brushes.xaml
Buttons.xaml
CheckBox.xaml

等等.

我们在 Bundle.Xaml 中加载它们:

we load them in 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 具有相同的文件:

B.Themes have the same Files:

Brushes.xaml
Buttons.xaml
CheckBox.xaml

我们在 Bundle.Xaml 中加载它们并添加 A.Themes 包:

we load them in Bundle.Xaml and adding the bundle of 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>

在我们的项目中,我们将它们加载到 App.xaml 中:

In our Project we load them in 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>

问题是:1. 它并不总是从 B.Themes 获取资源,我们无法找出原因.2. 如果我从 app.xaml 中删除对 A.Themes/Bundle.xaml 的引用,即使它包含在 B.Themes/Bundle.xaml 中,项目也无法从 A.Themes 中找到资源

the problems are: 1. it not always takes the resources from B.Themes and we can't find out why. 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

注意:我们参考了 B.Themes 中的 A.Themes 项目并参考主项目中的A.Themes和B.Themes

note: we have refernce to A.Themes project in B.Themes and refernce to A.Themes and B.Themes in main project

有人可以帮我了解这里发生了什么吗?谢谢!

can someone please help me to understande what is going on here? thanks!

推荐答案

加载顺序与您预期的不完全相同.来自 MSDN:

The loading order isn't quite what you expect. From 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

因此,合并到程序集 A 的 Bundle.xaml 中的字典实际上是在其他字典之后加载的.

So the dictionaries that merge into Bundle.xaml of assembly A are actually loaded after the other ones.

有关更多信息和相同行为的示例,请参阅以下链接:https://social.msdn.microsoft.com/Forums/vstudio/en-US/3bea80f9-d1db-4cb7-ae7a-77a02eaf4ec9/resourcedictionary-load-order?forum=wpf

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

这篇关于WPF 使用来自多个项目的多个资源字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆