Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

resourcedictionary - WPF Using multiple Resource Dictionaries from multiple projects

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...