我和这个SO问题以及这个问题遇到了完全相同的问题,但是被接受的解决方案对我无效。
我在我的app.xaml中声明了一个资源字典,如下所示(为了清晰起见进行了简化):
<Application.Resources>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skin/ResourceLibrary.xaml" />
<ResourceDictionary Source="Skin/Brushes/ColorStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
问题:
应用程序能够在app.xaml中列出时"看到"ColorStyles字典,但是如果我将它移动/嵌套到ResourceLibrary.xaml文件中,那么应用程序就无法"看到"ColorStyles.xaml,并出现有关缺少静态资源的错误。这是我创建ResourceLibrary.xaml字典的方法(简化版):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<!-- BRUSHES AND COLORS -->
<ResourceDictionary Source="Brushes/ColorStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
更改原因: 我目前的资源字典组织方式很糟糕,需要进行更改(因为我创建了多个对象)。我想在“Skin”文件夹中拥有一个资源字典,并使用子文件夹来组织其余的样式字典,这些字典都将合并在ResourceLibrary.xaml文件中,而该文件将在app.xaml中调用。
我的尝试: 是的,我确实尝试使用上面链接中的解决方案:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skin/ResourceLibrary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Dummy Style, anything you won't use goes -->
<Style TargetType="{x:Type Rectangle}" />
</ResourceDictionary>
</Application.Resources>
但是在虚拟样式行上我遇到了以下错误:
错误2 属性元素不能在元素的内容中间。它们必须在内容之前或之后。
将代码更改为以下内容即可消除上述错误,感谢lisp评论:
<Application.Resources>
<ResourceDictionary>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<!-- Dummy Style, anything you won't use goes -->
<Style TargetType="{x:Type Rectangle}" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skin/ResourceLibrary.xaml"></ResourceDictionary>
<ResourceDictionary Source="Skin/Brushes/ColorStyles.xaml" />
<ResourceDictionary Source="Skin/NamedStyles/AlertStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
但是资源库仍未被调用。
我还尝试将所有文件路径更改为打包URI,但这也没有解决问题。
我尝试将resourceLibrary.xaml和其他资源字典移动到不同的类库项目中(使用与上面相同的文件夹结构和文件)。然后我使用了以下URI,但仍无法访问ResourceLibrary.xaml文件中声明的资源。
<ResourceDictionary Source="pack://application:,,,/FTC.Style;component/ResourceLibrary.xaml" />
但是,如果我将每个资源字典按照上述UIR格式添加到App.Xaml文件中,则这些资源是可用的。
错误已经消失了,但我仍然无法使用ResourceLibrary.xaml文件中包含的合并字典的资源。我倾向于同意dowhilefor的评论,即我是否应该使用此方法,但我想找出原因,因为这个问题的最常见解决方案(请参见本帖子顶部的链接)不起作用,也许这个解决方案可以帮助其他人。
问题:为什么ResourceLibrary.xaml文件被忽略了?