Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用从另一个合并字典在合并字典中定义的样式_C#_Wpf_Xaml_Windows Phone 7_Windows Phone 8 - Fatal编程技术网

C# 使用从另一个合并字典在合并字典中定义的样式

C# 使用从另一个合并字典在合并字典中定义的样式,c#,wpf,xaml,windows-phone-7,windows-phone-8,C#,Wpf,Xaml,Windows Phone 7,Windows Phone 8,下面您可以看到我是如何通过合并字典来分离样式的(为了保持整洁,我跳过了名称空间) App.xaml: Colors.xaml: HeaderStyle.xaml: 在编译过程中,我遇到以下错误: 找不到名为/键为DarkTextForeground的资源 要使其正常工作,我们必须将Colors.xaml合并到HeaderStyle.xaml中,如下所示: <ResourceDictionary.MergedDictionaries> <ResourceDic

下面您可以看到我是如何通过合并字典来分离样式的(为了保持整洁,我跳过了名称空间)

App.xaml:


Colors.xaml:


HeaderStyle.xaml:


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

找不到名为/键为DarkTextForeground的资源

要使其正常工作,我们必须将Colors.xaml合并到HeaderStyle.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之前加载的,所以它应该在以后定义的词典中可见。

这是Erick Fleck在msdn论坛上的回复:

在第一个示例中,每个文件都被独立地解析,然后添加到合并词典中,这样它们就互不了解……类似地,合并词典中的XAML不能引用“父”ResourceDictionary中的名称。换句话说,您应该将MergedDictionaries视为单向引用


这就是它的工作方式,我想…

你说的是Visual Studio设计时间,对吗?实际项目将构建/运行正常,对吗?我能够在设计时通过引用App.xaml
应用程序中的合并词典来实现这一点。参考资料
@BrockHensley我有点困惑,因为在没有
MergedDictionary
的示例中,HeaderTyle.xaml:在VS design视图中一切正常(我可以看到带颜色的文本),我甚至可以成功构建项目,但当我尝试运行它时,我得到了XamlParseException:找不到名为/Key darktextfront.+1的资源,正如@Romasz所说的。静态资源是在编译时通过查看ResourceDictionary及其合并字典中的任何资源字典来解决的。它确实试着走近父母,查看兄弟姐妹的字典。我甚至不认为RD有一个指向“家长”字典的指针。哎呀,我的意思是说“它不试图向上走”。无论如何,这个问题似乎是答案,但我会修改我的评论,以防有人在以后阅读。
<SolidColorBrush x:Key="DarkTextForeground" Color="#7471b9"/>
<Style x:Key="HeaderTextBlockStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource DarkTextForeground}"/>
    <Setter Property="FontWeight" Value="Black"/>
</Style>
<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>