“用户控件的构造函数在C#中的参数 [英] 'UserControl' constructor with parameters in C#

查看:1365
本文介绍了“用户控件的构造函数在C#中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

叫我疯了,但我就是喜欢带参数的构造函数(如果需要的话)的人的类型,而不是一个构造函数不带参数,其次是设置属性。我的思维过程:如果属性都是必需的实际构造对象,他们应该在构造函数中。我得到两个好处:

Call me crazy, but I'm the type of guy that likes constructors with parameters (if needed), as opposed to a constructor with no parameters followed by setting properties. My thought process: if the properties are required to actually construct the object, they should go in the constructor. I get two advantages:


  1. 我知道,当构建一个对象(没有错误/例外),我的对象是不错的。

  2. 这有助于避免忘记设置一定的属性。

这心态也开始伤害我的问候,形成/用户控件的开发。想象一下用户控件:

This mindset is starting to hurt me in regards to form/usercontrol development. Imagine this UserControl:

public partial class MyUserControl : UserControl
{
  public MyUserControl(int parm1, string parm2)
  {
    // We'll do something with the parms, I promise
    InitializeComponent();
  }
}

在设计时,如果我一个窗体上放置该用户控件,我得到一个异常:

At designtime, if I drop this UserControl on a form, I get an exception:

无法创建组件的MyUserControl'...结果
  system.missingMethodException而 - 此对象定义无参数的构造函数

Failed to create component 'MyUserControl' ...
System.MissingMethodException - No parameterless constructor defined for this object.

好像对我来说,周围的唯一方法是添加默认的构造函数(除非别人知道的一种方式)。

It seems like, to me, the only way around that was to add the default constructor (unless someone else knows a way).

public partial class MyUserControl : UserControl
{
  public MyUserControl()
  {
    InitializeComponent();
  }

  public MyUserControl(int parm1, string parm2)
  {
    // We'll do something with the parms, I promise
    InitializeComponent();
  }
}

中不包括参数的构造函数的整点是要避免使用它。我甚至不能使用DesignMode属性做这样的事情:

The whole point of not including the parameterless constructor was to avoid using it. And I can't even use the DesignMode property to do something like:

public partial class MyUserControl : UserControl
{
  public MyUserControl()
  {
    if (this.DesignMode)
    {
      InitializeComponent();
      return;
    }

    throw new Exception("Use constructor with parameters");
  }
}

这不工作之一:

if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)

精细,顺动...

Fine, moving along ...

我有我的参数的构造函数,我可以放弃它的形式,而形式的的InitializeComponent会是这样的:

I have my parameterless constructor, I can drop it on the form, and the form's InitializeComponent will look like this:

private void InitializeComponent()
{
  this.myControl1 = new MyControl();

  // blah, blah
}

和信任我,因为我做到了(是的,忽略了产生Visual Studio中的注释),我想乱搞,我传递的参数来的InitializeComponent,这样我可以把它们传递给MyControl的构造函数。

And trust me, because I did it (yes, ignoring the comments Visual Studio generated), I tried messing around and I passed parameters to InitializeComponent so that I could pass them to the constructor of MyControl.

我这导致这个:

public MyForm()
{
  InitializeComponent(); // Constructed once with no parameters

  // Constructed a second time, what I really want
  this.myControl1 = new MyControl(anInt, aString);  
}

有关我使用一个带参数的用户控件的构造,我要补充一点,我不需要第二个构造?和实例化控制两次?

For me to use a UserControl with parameters to the constructor, I have to add a second constructor that I don't need? And instantiate the control twice?

我觉得我必须做一些错误的。思考?意见?保证(希望)?

I feel like I must be doing something wrong. Thoughts? Opinions? Assurance (hopefully)?

推荐答案

做关于道路Windows窗体的作品或多或少preclude参数.ctors Windows窗体组件的设计决策。您可以使用它们,但是当你做你踩普遍认可的机制之外。相反,Windows窗体通过属性值prefers初始化。这是一个有效的设计技术,如果没有得到广泛使用。

Design decisions made regarding the way Windows Forms works more or less preclude parameterized .ctors for windows forms components. You can use them, but when you do you're stepping outside the generally approved mechanisms. Rather, Windows Forms prefers initialization of values via properties. This is a valid design technique, if not widely used.

这有一定的好处,但。


  1. 使用为客户易于。客户端code并不需要跟踪一组数据,它可以立即创造一些,只是看到它与明智的(如果无趣)的结果。

  2. 使用为设计人员易于。设计师code是一般更清晰,更易于解析。

  3. 不鼓励单个组件中异常的数据依赖关系。 (虽然连微软自爆这一个用<一个href=\"https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=375805\"><$c$c>SplitContainer)

有在形式很多支持,在此技术也是设计师正常工作。事情是这样的<$c$c>DefaultValueAttribute, <一href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.aspx\"><$c$c>DesignerSerializationVisibilityAttribute,和<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.browsableattribute.aspx\"><$c$c>BrowsableAttribute给你的机会,提供以最小的努力丰富的客户体验。

There's a lot of support in forms for working properly with the designer in this technique also. Things like DefaultValueAttribute, DesignerSerializationVisibilityAttribute, and BrowsableAttribute give you the opportunity to provide a rich client experience with minimal effort.

(这不是是为在Windows窗体的客户端体验做出的唯一让步。抽象基类成分可能会很麻烦了。)

(This isn't the only compromise that was made for client experience in windows forms. Abstract base class components can get hairy too.)

我建议有一个参数的构造函数坚持和Windows窗体的设计原则下进行运作。如果确是preconditions你的用户控件必须强制,在另一个类封装它们,然后分配一个类的实例,以通过属性的控制。这将给一个值得关注的一点较好的分离为好。

I'd suggest sticking with a parameterless constructor and working within the windows forms design principles. If there are real preconditions that your UserControl must enforce, encapsulate them in another class and then assign an instance of that class to your control via a property. This will give a bit better separation of concern as well.

这篇关于“用户控件的构造函数在C#中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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