使用另一个合并字典的合并字典中定义的样式

时间:2014-02-08 13:26:13

标签: c# wpf xaml windows-phone-7 windows-phone-8

下面你可以看到我是如何通过合并字典来分隔样式的(我为了清洁而跳过命名空间)

的App.xaml

<Application.Resources>       
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Style/Colors.xaml" />
            <ResourceDictionary Source="Style/HeaderStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>            
    </ResourceDictionary>
</Application.Resources>

Colors.xaml

<SolidColorBrush x:Key="DarkTextForeground" Color="#7471b9"/>

HeaderStyle.xaml

<Style x:Key="HeaderTextBlockStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource DarkTextForeground}"/>
    <Setter Property="FontWeight" Value="Black"/>
</Style>

在编译过程中,我收到以下错误:

  

找不到名称/密钥DarkTextForeground的资源

要使其工作,我们必须在 HeaderStyle.xaml 中合并 Colors.xaml ,如下所示:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style x:Key="HeaderTextBlockStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource DarkTextForeground}"/>
    <Setter Property="FontWeight" Value="Black"/>
</Style>

任何人都可以向我解释,为什么我必须在 HeaderStyle.xaml 中引用 Colors.xaml
我不能只引用不同的定义样式合并字典?
我假设 Colors.xaml HeaderStyle.xaml 之前加载,因此对于稍后定义的字典应该是可见的。

1 个答案:

答案 0 :(得分:5)

这是对来自msdn论坛的Erick Fleck的my question的回复:

  

在你的第一个例子中,每个文件都是独立解析的,然后被添加到合并的字典中,这样他们就不知道彼此有什么了...同样,合并字典中的XAML也不能引用'父母中的名字'ResourceDictionary。换句话说,您应该将MergedDictionaries视为单向引用。

这就是它的工作方式我猜...

相关问题