WPF编译错误“IDictionary must have a Key attribute" [英] WPF compile error "IDictionary must have a Key attribute"
问题描述
我已经创建了要在 WPF 应用程序中的多个 xaml 页面中使用的控件样式.为此,我创建了一个 Resources.xaml 并在其中添加了样式.
I've created control styles I want to use among multiple xaml pages in my WPF app. To do this I created a Resources.xaml and added the styles there.
然后在我的页面中添加此代码
Then in my pages I add this code
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SampleEventTask;component/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
在两页上这工作正常,但在第三页上我收到一个编译错误:
On two pages this works fine, but on the 3rd page I get a compile error that says:
添加到 IDictionary 的所有对象必须有一个 Key 属性或一些关联的其他类型的密钥
All objects added to an IDictionary must have a Key attribute or some other type of key associated with them.
如果我为此添加一个键,例如 ResourceDictionary x:Key="x",那么编译错误会继续,但在运行应用程序时,它会错误地找到样式.
If I add a key to this, as such ResourceDictionary x:Key="x", then the compile error goes but on running the app it errors finding the style.
我可以使编译错误消失,并通过将原始(未指定键)ResourceDictionary"xaml 从顶级网格移动到该页面上包含的网格来运行应用程序.
I can make the compile error go away and have the app run by just moving original (no key specified) "ResourceDictionary" xaml from the top level Grid into a contained Grid on that page.
但我不明白这里发生了什么.关于问题是什么的任何建议,我只是遗漏了一些东西或做错了一些事情.有没有更好的方式分享样式?
But I don't understand what is going on here. Any suggestions as to what the problem is, I'm just missing something or doing something incorrectly. Is there a better way to share styles?
谢谢
推荐答案
在该页面中除了合并的 ResourceDictionary 之外,是否还定义了任何其他资源?
Are there any other resources defined other than the merged ResourceDictionary in that Page?
例如,这是我创建的一个窗口的片段.
For example, here's a snippet from a Window I created.
<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> ...
合并后的字典不需要键,但该窗口的其他资源需要.
The merged dictionary did not require a key, but the other resources for that Window do.
这篇关于WPF编译错误“IDictionary must have a Key attribute"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!