2017-01-12 4 views
0

디자인 타임에 구성 요소에 새 속성을 추가하려고합니다. 이 속성은 디자인 뷰에 표시되지만 값을 수정할 수 없으며 "개체 참조가 개체의 인스턴스로 설정되지 않았습니다"로 표시됩니다. 속성을 인스턴스화해야하는 경우 MSDN과 Google이 실패합니다.내가 추가하는 디자인 타임 구성 요소 속성에 디자인보기에서 변경할 수없는 값으로 null 참조 오류가 있습니다.

어디로 잘못 가고 있습니까? 다음은 문제를 나타내는 코드의 약식 버전입니다.

[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); 
     } 
    } 
} 

답변

0

디자이너 클래스는 적절한 GET 및 설정 기능과 속성을 구현하고, 아래의 코드와 같이, 속성에 대한 초기 값을 포함하도록 오버라이드 (override) 할 필요가 초기화 할 필요가있다.

[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; 
      } 
     } 
    } 
} 

자세한 내용은 this MSDN article을 확인하십시오.