繁体   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