自定义属性默认值和放大器; .NET设计 [英] Custom Property Default Value & .NET Designer

查看:110
本文介绍了自定义属性默认值和放大器; .NET设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是避免(或控制)的初始化在.NET中的重定义属性的设计师的最佳方法是什么?有时,它有一个属性设置为最初的东西没有这种设置被行动最初设定时,在很重要的。

在下面的假想的例子,我想实现有类似UpdateSetting作为属性的灵活性,但其数据库中设置为零,每次不是不便的应用程序启动。所有我能想到的是打开一个标志,另一个属性。

 公共类答:用户控件
{
  公众诠释UpdateSetting
  {
    //写入数据库或一些这样的事情
  }

...

  公共无效的Ini​​tializeComponent()
  {
    A中的=新的A();
    a.UpdateSetting = 0; //导致数据库写入
  }
}
 

解决方案

假设你控制组件(A类),申请DesignerSerializationVisibilityAttribute到昂贵的财产,与隐藏选项说不产生二传这个属性:

 公共类答:用户控件
{
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
  公众诠释UpdateSetting
  {
    //写入数据库或一些这样的事情
  }
}
 

(编辑:在重新阅读我不知道你是否要prevent初始化由设计师,或使之选,或允许其通过设计师来完成,但推迟初始化的实际执行这不仅解决了第一项方案。)

What's the best way to avoid (or control) the initialisation by the designer of a heavy custom property in .NET? Sometimes it is important to have a property set to something initially without that setting being acted upon when initially set.

In the imaginary example below, I want to achieve the flexibility of having something like UpdateSetting as a property, but not the inconvenience of having the database set to zero every time the application starts up. All I can think of is another property that unlocks a flag.

public class A : UserControl
{
  public int UpdateSetting 
  {
    // Write to database or some such thing
  }

...

  public void InitializeComponent()
  {
    A a = new A();
    a.UpdateSetting = 0;  // Causes database write
  }
}

解决方案

Assuming you control the component (the A class), apply DesignerSerializationVisibilityAttribute to the expensive property, with the Hidden option to say "don't generate a setter for this property":

public class A : UserControl
{
  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  public int UpdateSetting 
  {
    // Write to database or some such thing
  }
}

(EDIT: On re-reading I'm not sure whether you want to prevent initialisation by the designer, or make it optional, or allow it to be done through the designer but defer the actual execution of the initialisation. This addresses only the first of those scenarios.)

这篇关于自定义属性默认值和放大器; .NET设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆