2017-08-15 20 views
1

使用多個資源字典我有兩個類庫項目: 項目A.Themes 項目B.ThemesWPF從多個項目

項目A.Themes是我的基地主題項目。 項目B.Themes使用A.Themes和有新的樣式和一些資源有已經在A.Themes中定義的鍵。

我們希望在我們的項目中使用這兩個主題,如果我們使用在兩個項目中定義的資源,我們想從B.Themes中獲取資源。

這是我們的代碼:

A.Themes有款式幾個文件:

Brushes.xaml 
Buttons.xaml 
CheckBox.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有相同的文件:

Brushes.xaml 
Buttons.xaml 
CheckBox.xaml 

我們加載它們在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> 

在我們的項目中,我們加載它們在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無法找到A.Themes資源,即使它包含在B.Themes/Bundle.xaml

注: 我們參考B.Themes中的A.Themes項目 並參考主題中的A.Themes和B.Themes項目

有人可以幫我理解這裏發生了什麼嗎? 謝謝!

+0

我的猜測:你可能對合並資源字典沉默異常,因爲你有重疊的資源名稱。一旦我有這樣的事情(雖然錯誤來源不同),我用IlSpy反編譯應用程序。它沒有設法反編譯一些資源字典(或部分做了,現在不記得了),這幫助我追蹤根源。 –

+0

我看到我有錯誤「值不能爲空。參數名稱:項目」。在B.Themes/Bundle.xaml – StackOverflowUser

回答

2

加載順序並不完全符合您的期望。從MSDN:

資源合併字典在資源查找範圍,只是它們被合併到

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

所以主要資源字典的範圍後佔據一個位置彙編到A的Bundle.xaml的字典實際上被加載到其他的字符之後。

請參考以下鏈接瞭解更多信息和相同行爲的一個例子:https://social.msdn.microsoft.com/Forums/vstudio/en-US/3bea80f9-d1db-4cb7-ae7a-77a02eaf4ec9/resourcedictionary-load-order?forum=wpf

+0

中的加載A.Themes/Bundle.xaml的行中。「因此,合併到程序集A的Bundle.xaml中的字典實際上是在其他程序之後加載的。」這就是我想要的,但它不會 – StackOverflowUser

+0

不,這不是你想要的。你想讓B字典在A之後加載,不是嗎? – mm8

+0

oopes,是的。對不起,但我不明白我應該怎麼做 – StackOverflowUser