gpt4 book ai didi

c# - 使用来自另一个合并词典的合并词典中定义的样式

转载 作者:太空狗 更新时间:2023-10-29 21:39:26 26 4
gpt4 key购买 nike

下面你可以看到我是如何尝试通过合并字典来分离样式的(为了简洁起见,我跳过了命名空间)

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>

在编译过程中出现以下错误:

Cannot find a Resource with the Name/Key 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.xamlHeaderStyle.xaml 之前加载,因此它应该对稍后定义的字典可见。

最佳答案

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

In your first example each file is parsed independently and then added to the merged dictionary so they don't know anything about each other...similarly, the XAML in a merged dictionary cannot reference names in the 'parent' ResourceDictionary. In other words, you should think of a MergedDictionaries as a one-way reference.

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

关于c# - 使用来自另一个合并词典的合并词典中定义的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21646544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com