【问题标题】:Each dictionary entry must have an associated key每个字典条目必须有一个关联的键
【发布时间】:2014-02-05 01:54:16
【问题描述】:

我有一个继承 IDictionary 的类,名为 ResourceDictionary
另外我还有另一个类,其属性为DictionaryKeyProperty,名为Style
DictionaryKeyProperty 被命名为 TargetType

XAML 文件内容:

<ResourceDictionary
             xmlns="clr-namespace:Test;assembly=Test"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    <Style TargetType="test" />
</ResourceDictionary>

它给出了错误 Each dictionary entry must have an associated key.

public class ResourceDictionary : IDictionary, ICollection, IEnumerable, INameScope, ISupportInitialize
{
   ...
}

[ContentProperty("Setters"), DictionaryKeyProperty("TargetType")]
public class Style : Sealable, INameScope, IQueryAmbient, IResources
{

    private Type _TargetType;
    [Ambient]
    public Type TargetType
    {
        get { return _TargetType; }
        set
        {
            CheckSealed();
            if (value == null)
                throw new ArgumentNullException("value");
            _TargetType = value;
        }
    }

    ......

}

我做错了什么?
我怎么解决这个问题?

P.S.:我想制作一个包含 WPF 等依赖系统的轻量级框架。

【问题讨论】:

    标签: c# xaml


    【解决方案1】:

    XAML 编译器抱怨,因为您的样式定义缺少 Key 属性。

    改成:

    <Style x:Key="someKey" TargetType="test" />
    

    .. 将使错误消失,但由于我怀疑您想为控件声明默认样式,因此需要您始终使用该样式引用该样式

    <Test Style="{StaticResource someKey}" />
    

    这可能不是你的想法。

    你能用“test”类的代码更新问题吗?

    【讨论】:

    • 这是一个答案吗..?还是一个新问题?我无法决定.. 我可以建议你删除这个问题并解释他如何使用Dictionary 来代替吗?
    • 我想创建一个小框架,只使用System.Xaml,不包含WPF。
    • 请提供更多详细信息。我不能跟着你。
    • 'DictionaryKeyProperty' 用于将“属性”设置为“IDictionary”集合的“x:Key”。我的 Xaml 内容构建成功,它也可以加载。但它只是在 XAML 设计器中显示错误。
    猜你喜欢
    • 2013-12-09
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    相关资源
    最近更新 更多