簡體   English   中英

WPF使用來自多個項目的多個資源字典

[英]WPF Using multiple Resource Dictionaries from multiple projects

我有兩個班級的圖書館項目:項目A.Themes項目B.Themes

項目A.Themes是我的基本主題項目。 使用A.Themes的項目B.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的引用,即使該項目包含在B.Themes / Bundle.xaml中,該項目也無法從A.Themes中找到資源。

注意:我們在B.Themes中引用了A.Themes項目,並且在主項目中引用了A.Themes和B.Themes。

有人可以幫我了解一下這里的事嗎? 謝謝!

加載順序與您期望的不太一樣。 從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階?論壇= WPF

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM