wpf “Key”属性只能用于包含在“IDictionary”中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14907524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:33:34  来源:igfitidea点击:

The "Key" attribute can only be used on an element that is contained in "IDictionary"

wpfxaml

提问by Brandon Moore

I'm getting the squiggly line under the 6th line of code below giving me the error stated in the title. I just migrated to VS 2012 and everything was working fine in VS 2010. I feel like maybe the problem is really elsewhere... can someone tell me if there is actually something wrong with this xaml?

我在下面的第 6 行代码下看到了波浪线,这给了我标题中所述的错误。我刚刚迁移到 VS 2012,并且在 VS 2010 中一切正常。我觉得问题可能真的出在其他地方......有人可以告诉我这个 xaml 是否真的有问题吗?

<Application x:Class="SageWpf.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:SageWpf">
    <Application.Resources>
        <ResourceDictionary x:Key="rd">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:AppBootStrapper x:Key="bootstrapper"/>
                    <local:EffectConverter x:Key="effectConverter"/>
                    <local:VisibilityConverter x:Key="visibilityConverter"/>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>


    </Application.Resources>
</Application>

回答by Federico Berasategui

<ResourceDictionary x:Key="rd">is invalid. Remove the x:Keyfrom there.

<ResourceDictionary x:Key="rd">是无效的。x:Key从那里删除。

Also.. that's a bad way to structure your resources. Change it to:

另外..这是构建资源的一种糟糕方式。将其更改为:

<Application.Resources>
    <ResourceDictionary>
        <local:AppBootStrapper x:Key="bootstrapper"/>
        <local:EffectConverter x:Key="effectConverter"/>
        <local:VisibilityConverter x:Key="visibilityConverter"/>
    </ResourceDictionary>
</Application.Resources>

Only use Merged Dictionaries if you have resources defined in another XAML file and you want to import them here.

如果您在另一个 XAML 文件中定义了资源并且想要在此处导入它们,则仅使用合并词典。