在Silverlight中从不同的ResourceDictionary引用另一个ResourceDictionary中的资源

15

我在我的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资源字典。

我可以从其他控件引用该画笔,但无法从包含的资源字典中引用。

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

3个回答

14
你是否在NavigationPanel字典中的任何资源中引用了DarkBackground画刷?如果是,你可能需要将Brushes资源字典合并到NavigationPanel字典中。因此,在NavigationPanel字典中进行合并操作。
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>

1
这确实有效,我现在明白为什么它能解决问题了,但为什么当它在更高级别的字典中时就无法解决呢?我会将您的答案标记为正确,但如果您知道原因,我很想听一下解释。 - Mike S.
1
在另一个帖子中找到了这个。为了后代留在这里。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 感谢和赞誉Kamil Nieweglowski。 - Snouto

9
您可以像C#中的“using”一样将一个字典包含在另一个字典中,方法如下:
<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》项目模板有一个很好的例子...


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接