[英]The design-time component property I add has a null reference error as an immutable value in design view
我正在尝试在设计时将新属性添加到组件。 该属性在设计视图中可见,但是无法修改该值,并显示为“对象引用未设置为对象的实例”。 如果我需要实例化该属性,则MSDN和Google会让我失望。
我要去哪里错了? 这是我正在使用的代码的缩写版本,用于演示该问题。
[DesignerAttribute(typeof(designPropDesigner))]
public class designProp : Component
{
public class designPropDesigner : ComponentDesigner
{
protected override void PreFilterProperties(IDictionary properties)
{
base.PreFilterProperties(properties);
var prop = TypeDescriptor.CreateProperty(typeof(designPropDesigner), "prop", typeof(string), new Attribute[] { DesignOnlyAttribute.Yes, new DefaultValueAttribute("") });
properties.Add("prop", prop);
}
}
}
设计器类需要使用适当的get和set函数来实现该属性,并且应该重写initialize,以包括该属性的初始值,如下面的代码所示。
[DesignerAttribute(typeof(designPropDesigner))]
public class designProp : Component
{
public class designPropDesigner : ComponentDesigner
{
private string _prop;
public override void Initialize(IComponent component)
{
base.Initialize(component);
this.prop = "value";
}
protected override void PreFilterProperties(IDictionary properties)
{
base.PreFilterProperties(properties);
var prop = TypeDescriptor.CreateProperty(typeof(designPropDesigner), "prop", typeof(string), new Attribute[] { DesignOnlyAttribute.Yes, new DefaultValueAttribute("") });
properties.Add("prop", prop);
}
private string prop
{
get
{
return _prop;
}
set
{
_prop = value;
}
}
}
}
有关其他信息,请查看此MSDN文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.