无法引用包含合并字典的资源字典

我有一个库,CommonLibraryWpfThemes,里面有几个Resource Dictionary XAML文件。 我的主题/ Generic.xml文件包含将所有其他文件合并在一起的ResourceDictionary.MergedDictionaries声明。

Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" /> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" /> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" /> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" /> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> 

在我的应用程序项目中,我引用了CommonLibraryWpfThemes,并且在App.xaml文件中明确引用了Generic.xml。

App.xaml – 失败

 <Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" /> </Application.Resources> </Application> 

这不起作用。 我运行我的应用程序时出现以下错误:

 System.Windows.Markup.XamlParseException occurred Message="Cannot find resource named '{_fadedOrangeBrush}'. Resource names are case sensitive. Error at object 'System.Windows.Setter' in markup file 'CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml' Line 18 Position 13." Source="PresentationFramework" LineNumber=18 LinePosition=13 

如果我把Generic.xaml的内容直接放到App.xaml中,一切正常。

App.xaml – 成功

 <Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" /> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" /> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" /> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" /> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> 

也许我会以错误的方式去解决这个问题。 我的目标是简化从多个应用程序中引用我所有的主题资源,而不必列出所有单个文件。 有没有推荐的方法来做到这一点? (注:我不想在多个主题之间切换 – 我只有一个主题。)

作为奖励,如果有人能够告诉我如何引用外部库中的资源而不打破Visual Studio中的devise器,那将是一件好事。

谢谢。

编辑:

我尝试在ResourceDictionary.MergedDictionary元素包装ResourceDictionary,但也没有工作(我得到相同的错误):

 <Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> 

前面回答了类似的问题,请参阅将合并字典添加到合并字典问题。

这是一个优化错误,请参阅内部MergedDictionaries中未find的Microsoft Connect / DefaultStyleKey样式 :

在创buildXAML中的每个对象时,如果存在默认样式(即样式w /一个键types),该样式应该应用。 正如你可以想象有几个性能优化,使(隐含)查找尽可能轻。 其中之一就是我们不会查看资源字典,除非它们被标记为“包含默认样式”。 有一个错误:如果所有默认样式嵌套在合并字典中,则顶层字典的三个深度(或更深)不会被标记,因此search将跳过它。 解决的办法是把一个默认的风格,以任何东西,在根词典。

所以添加一个虚拟样式到根字典修复了这个问题。 例

 <Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" /> </ResourceDictionary.MergedDictionaries> <!-- Dummy Style, anything you won't use goes --> <Style TargetType="{x:Type Rectangle}" /> </ResourceDictionary> </Application.Resources> </Application> 

检查App.xaml.cs中的构造函数调用InitializeComponent() – 这是什么合并资源字典…

你根本不需要引用generic.xaml ,它有内置的支持。 但是,这意味着它提供了默认样式,而不是显式设置。 显式设置样式/模板需要从明确引用的字典中获得。

(编辑清晰)

一个例外是App.xaml ,其中定义的资源可以被整个应用访问,而不需要引用任何特定的资源字典。 资源本身必须可以通过名称访问。

这失败的原因

 <Application.Resources> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" /> </Application.Resources> 

是,我想,因为你没有把它包装在一个MergedDictionary包装,将其添加到合并字典。 直接向资源添加仅适用于本地声明的资源,例如样式等。

然而,正如我之前所说的,你不应该在任何地方合并generic.xaml ,也许你应该重构刷式和样式外使用的其他资源,只合并app.xaml资源。

还要注意样式不一定要在generic.xaml中具有“默认样式”的行为 – 如果样式的键值等于元素的types(全局或本地资源),那么它将使用作为默认样式的样式。 generic.xaml只是一个方便。

检查这个答案。

对于其他自定义画笔等,您需要明确引用这些资源。

你也应该检查WindowDictionary.xaml的内容,这个错误有一定的异味。

我在unit testing中遇到了这个错误,Chris的回答给了我需要的线索。 基本上我的第一个testing方法,我把:

  MyApplication.App app = new MyApplication.App(); app.InitializeComponent(); 

突然间,它可以find我的网页模板。 注意:这意味着你必须检查你的App的实例是否已经存在,如果你unit testing你的App.cs。

我的解决scheme是在这里 ,单击变通办法。