gpt4 book ai didi

WPF编译错误 "IDictionary must have a Key attribute"

转载 作者:行者123 更新时间:2023-12-01 23:23:46 25 4
gpt4 key购买 nike

我已经创建了想要在 WPF 应用程序的多个 xaml 页面中使用的控件样式。为此,我创建了一个 Resources.xaml 并在其中添加了样式。

然后在我的页面中添加此代码

<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SampleEventTask;component/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>

在两个页面上,这工作正常,但在第三页上,我收到一个编译错误,内容如下:

All objects added to an IDictionary must have a Key attribute or some other type of key associated with them.

如果我为此添加一个键,例如 ResourceDictionary x:Key="x",则会出现编译错误,但在运行应用程序时会错误地查找样式。

我可以通过将原始(未指定键)“ResourceDictionary”xaml 从顶级网格移动到该页面上包含的网格来消除编译错误并运行应用程序。

但是我不明白这里发生了什么。关于问题是什么的任何建议,我只是错过了一些东西或做错了一些事情。有更好的方式来共享样式吗?

谢谢

最佳答案

除了合并的 ResourceDictionary 之外,该页面中是否还定义了任何其他资源?

例如,这是我创建的窗口的片段。

<Window x:Class="SelectionPagePrototype.SelectionPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SelectionPagePrototype"
Title="SelectionPage" MinHeight="600" MinWidth="800" Loaded="OnLoaded">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ImageResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
<local:QuickPickCheckedConverter x:Key="quickPickConverter" />
<local:BoolToCaptionConverter x:Key="captionConverter" />
<local:ProductAndImageTypeConverter x:Key="imageConverter" />
</ResourceDictionary>
</Window.Resources>
<Grid> ...

合并的字典不需要键,但该窗口的其他资源需要。

关于WPF编译错误 "IDictionary must have a Key attribute",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2931864/

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