【问题标题】:Referencing a resource in a ResourceDictionary from a different ResourceDictionary in Silverlight从 Silverlight 中的不同 ResourceDictionary 引用 ResourceDictionary 中的资源
【发布时间】:2012-02-29 06:47:58
【问题描述】:

我的 App.xaml 中有以下代码集:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/Fonts.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/CoreStyles.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/SdkStyles.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/MyAppName.xaml"/>

            <ResourceDictionary Source="/Client.Common;component/Controls/NavigationPanel.xaml"/>
         </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

NavigationPanel.xaml 包含如下样式:

<Style x:Key="NavigationPanelListBox" TargetType="ListBox">
    <Setter Property="Background" Value="{StaticResource DarkBackground}" />
    <Lots of XAML>
</Style>

{StaticResource DarkBackground}Brushes.xaml 文件(即第一个资源字典)中定义。它被定义为

<SolidColorBrush x:Key="DarkBackground" Color="#FF707176" />

在资源字典中。

在运行时,我收到以下错误:

Cannot find a Resource with the Name/Key DarkBackground [Line: 16 Position: 44]

行号和位置引用app.xaml中的NavigationPanel.xaml资源字典。

我可以从其他控件引用画笔,但不能引用包含的资源字典。

为什么我不能引用或为什么不能解析对合并资源字典层次结构中更高资源的引用?我在这里错过了什么?

【问题讨论】:

    标签: c# silverlight xaml resourcedictionary mergeddictionaries


    【解决方案1】:

    您是否在NavigationPanel 字典的任何资源中引用了DarkBackground 画笔?

    如果您是,您可能需要将 Brushes 资源字典合并到 NavigationPanel 字典中。

    所以在 NavigationPanel 字典中。

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml" />
    </ResourceDictionary.MergedDictionaries>
    

    【讨论】:

    • 这确实有效,我理解为什么它现在可以解决,但是为什么它在更高级别的字典中时不能解决?我将您的答案标记为正确,但如果您知道这是为什么,我希望能得到解释。
    • 在另一个线程中找到了这个。留在这里留给后人。 social.msdn.microsoft.com/forums/windowsapps/en-US/… 感谢并感谢 Kamil Nieweglowski 的贡献。
    【解决方案2】:

    您可以将一个字典包含在另一个字典中(如 C# 中的“使用”),如下所示:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
        xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:Controls="clr-namespace:APC.IKM.UI.SL.Controls"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Brushes.xaml"/>
            <ResourceDictionary Source="Fonts.xaml"/>
            <ResourceDictionary Source="CoreStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    

    这就是你要找的吗? Cosmopolitan / Metro 项目模板就是一个很好的例子……

    【讨论】:

      【解决方案3】:

      真正的答案是 Eric 在这个网站上的答案:https://social.msdn.microsoft.com/forums/windowsapps/en-US/2be9a5f6-5313-448d-a9d9-296bac42215e/using-style-defined-in-merged-dictionary-from-another-merged-dictionary?forum=wpdevelop
      Brushes.xaml 和 NavigationPanel.xaml 被独立解析,然后添加到应用程序资源的合并字典中,因此它们彼此不知道任何信息。

      【讨论】: