使用在合并字典定义的样式从另一个合并词典 [英] Using style defined in merged dictionary from another merged dictionary
问题描述
下面你可以看到我是如何我试图通过合并字典(我跳过清洁度的缘故命名空间)
Below you can see how I am trying to segregate styles by merging dictionaries (I'm skipping namespaces for the sake of cleanliness)
的 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>
在编译过程中,我收到了以下错误:
During compilation I get a following error:
无法找到名称/主要DarkTextForeground
Cannot find a Resource with the Name/Key DarkTextForeground
要使它发挥作用,我们必须合并的 HeaderStyle.xaml 的里面的 Colors.xaml 的是这样的:
To make It work we have to merge Colors.xaml inside HeaderStyle.xaml like this:
<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>
谁能给我解释一下,为什么我要引用的 Colors.xaml 的在 HeaderStyle.xaml 的?
我不能只是引用不同的合并字典定义的样式?
我假设的 Colors.xaml的之前加载的 HeaderStyle.xaml 的,所以应该是后面定义词典可见。
Can anyone explain to me, why I have to reference Colors.xaml in HeaderStyle.xaml?
Can't I just reference styles defined in different merged dictionary?
I assume that Colors.xaml is loaded before HeaderStyle.xaml so it should be visible for dictionaries defined later.
推荐答案
这是的从埃里克·弗莱克我的问题在 MSDN论坛:
This is a response to my question from Erick Fleck at msdn forums:
的在你的第一个例子中,每个文件独立解析,然后添加到合并字典所以他们不知道彼此......同样的,在合并字典XAML中的父的ResourceDictionary不能引用名什么。换句话说,你应该想到MergedDictionaries为单向参考。的
这是它的工作原理我的方式猜...
It's the way It works I guess...
这篇关于使用在合并字典定义的样式从另一个合并词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!