c# - 每个字典条目必须有一个关联的键

标签 c# xaml

我有一个继承 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 之类的依赖系统的轻量级框架。

最佳答案

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

将其更改为:

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

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

引用该样式
<Test Style="{StaticResource someKey}" />

这可能不是您想要的。

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

关于c# - 每个字典条目必须有一个关联的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18671317/

相关文章:

c# - Windows 服务未完全启动

c# - 如何以编程方式将 C# 中的图像源设置为 XAML 静态资源?

c# - 尝试设置 TextBox.IsReadOnly 时出现奇怪的 XAML 解析错误

c# - WPF:同步 ItemsControl 中所有项目的宽度

c# - 为什么 TcpListener 没有实现 IDisposable

c# - 定义 : What is a HashSet?

c# - Wpf 托管 Windows 窗体 - 鼠标事件未通过

c# - 在 Windows Phone 8.1 XAML 中获取设备屏幕分辨率

c# - WPF 中的简单数据绑定(bind)

c# - 使用 Internet Explorer 的 asp.net 登录控件