我有一个继承 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/