WPF Using multiple Resource Dictionaries from multiple projects

ぐ巨炮叔叔 提交于 2019-11-29 23:54:25

问题


I have two class Library projects: Project A.Themes Project B.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.

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.

This is our code:

A.Themes have few files of styles:

Brushes.xaml
Buttons.xaml
CheckBox.xaml

etc..

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 have the same Files:

Brushes.xaml
Buttons.xaml
CheckBox.xaml

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>

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>

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

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!


回答1:


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

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

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://stackoverflow.com/questions/45687750/wpf-using-multiple-resource-dictionaries-from-multiple-projects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!