WPF Using multiple Resource Dictionaries from mult

2019-04-02 10:20发布

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条回答
▲ chillily
2楼-- · 2019-04-02 11:04

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

查看更多
登录 后发表回答