【发布时间】:2013-06-12 04:57:50
【问题描述】:
我有一个 WPF .net 4.5 应用程序,在合并资源字典时遇到问题。
我有与This SO question 和This Question 完全相同的问题,但接受的解决方案对我不起作用。
我的 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" />
但同样,如果我将每个资源字典添加到 App.Xaml 文件中,使用上面的 UIR 格式,这些资源都是可用的。
错误消失了,但我仍然无法使用属于 ResourceLibrary.xaml 文件中合并字典一部分的资源。我倾向于同意 dowhilefor 关于我是否应该使用这种方法的评论,但我想弄清楚这一点,因为这个问题的最常见解决方案(参见本文顶部的链接)不起作用,也许这个解决方案可以帮助别人。
问题:为什么 ResourceLibrary.xaml 文件会被忽略?
【问题讨论】:
-
您的虚拟样式相关的 xaml 是否与发布的完全一样?我没有
Error 2,您的原始代码中可能有语法错误,请参阅this -
@lisp 谢谢,我编辑了我的代码以显示实际发生的情况。我在合并字典的上方和下方都声明了元素。所以我更改了代码,但我仍然遇到 ResourceLibrary 没有被调用的问题
-
我可以给你一个很好的提示:使用尽可能少的 xamls。不要试图通过拆分来过度组织它。我们用 500 个 xamls 完成了它,我仍然在晚上哭着和出汗醒来。您最多应该有 3 层。 app.xaml,其中包含所有资源字典、来自具有默认控件的库的 Generic.xaml,当然还有您在 app.xaml 中“包含”的尽可能少的“正常”xaml。请记住,在合并 xamls 时,它们的顺序非常重要。
-
我们一开始这样做是因为我们需要更高的维护,并且我们认为将其拆分为多个 xamls 对我们来说更容易处理。最后,我们的内存消耗非常高,性能也很糟糕。因此,我们必须恢复我们在 2 年内创建的所有这些 xamls,并将其恢复到现在的位置。最大的问题是有一个共享或styles.xaml 并将其“包含”在每个其他xaml 中。永远不要这样做。在我的旧posts 上查看有关该问题的一个。
-
我的知识还是有点模糊,但据我了解,不,你只创建一次资源,但试图找到它们需要更长的时间,因为不同的字典要遍历多次。如果您有 20 个 xaml 并且都以正确的顺序合并到 app.xaml 中,那么一切都应该没问题。将它们合并到一个 xaml 会更好,但在那之后我没有注意到任何巨大的增长。
标签: .net wpf xaml resourcedictionary