【问题标题】:Each dictionary entry must have an associated key error message每个字典条目都必须有一个关联的键错误消息
【发布时间】:2017-02-25 07:54:35
【问题描述】:

有人可以帮我解决这个问题吗,我想我已经正确编码了所需的密钥。这是一个样式应用程序字典测试,只有一个按钮。有两个错误消息:每个字典条目必须有一个关联的键,并且所有添加到 IDictionary 的对象都必须有一个 Key 属性或与之关联的其他某种类型的键。第 13 行位置 14,均用于 MainWINdow.xaml。

此项目中没有程序员编写的代码。

这是 MainWindow.xaml 代码:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary Source="App.xaml" /> This is the offending line
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource algo}" />
</Grid>

这是 App.xaml 代码:

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApplication1"
         StartupUri="MainWindow.xaml">
<Application.Resources>
        <Style x:Key="algo" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Red" />
        </Style>
</Application.Resources>

【问题讨论】:

  • 违规行应该在ResourceDictionary.MergedDictionaries内。否则它将被视为字典中的条目。 Documentation

标签: wpf app.xaml


【解决方案1】:

正如 cmets 中所指出的,引用 ResourceDictionary 的语法是

<Window.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="myresourcedictionary.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Window.Resources>

ResourceDictionary 的直接内容被认为是字典的条目。这就是缺少的Key 错误所指的地方。字典应该查找所有条目的键:

<Window.Resources>
    <ResourceDictionary>
        <conv:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
        <conv:BoolToCollapsedConverter  x:Key="BoolToCollapsedConverter" />
        ...
    </ResourceDictionary>
</Window.Resources>

该规则的一个例外是 implicit Styles(具有 TargetType 而不是 Key 的样式)。

在您的情况下,上述方法均无济于事,因为 App.xaml 中的 Resources 被视为特殊。它们被视为全球资源,可以从任何地方引用。尝试像第一个示例中那样明确引用它们将导致

查找资源字典“App.xaml”时出错。

改为将您的 MainWindow.xaml 更改为

<Window x:Class="WpfApplication1.MainWindow"
    ...
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Style="{StaticResource algo}" />
</Grid>

【讨论】:

    【解决方案2】:

    谢谢Funk,谢谢Mathew Jibin,你的解释让我找到了我认为的解决方案,如下:

    将 App.xaml 修改为:

        <Application.Resources>
            <Style TargetType="Button">
                <Setter Property="Background" Value="Red" />
            </Style>
        </Application.Resources>
    

    并修改 MainWindow 删除 Grid 中的所有内容:

        ...  
        Title="MainWindow" Height="350" Width="525">
        <Grid>
        </Grid>
    

    现在,每次我创建一个新按钮时,它都有所需的样式。如果您发现此解决方案有任何可能的问题,请告诉我。谢谢。

    【讨论】:

    • 没错,如果您希望Control 的所有实例都受到影响,那么隐式样式(如您的样式)是可行的方法。否则,您将给样式一个Key 并在Control 中引用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多