简体   繁体   English

WinForms:UserControl DesignTime:使用哪种方法?

[英]WinForms: UserControl DesignTime: which method to use?

I found this answer on Stackoverflow: DesignMode with NestedControls .我在 Stackoverflow 上找到了这个答案: DesignMode with NestedControls

The best answer stated that, if you don't want to use reflection, and you want to check in the constructor whether you are in DesignMode, you should use something like:最好的答案是,如果你不想使用反射,并且你想在构造函数中检查你是否处于设计模式,你应该使用类似的东西:

bool inDesignMode = System.ComponentModel.LicenseManager.UsageMode == LicenseUsageMode.Designtime;

My Problem我的问题

To test this, I created a simple usercontrol with three Labels:为了测试这一点,我创建了一个带有三个标签的简单用户控件:

public MyUserControl()
{
    InitializeComponent();

    this.label1.Text = String.Format("Construction: DesignMode: {0}", this.DesignMode);

    bool inDesignMode = System.ComponentModel.LicenseManager.UsageMode == LicenseUsageMode.Designtime;
    this.label2.Text = String.Format("Construction: LicenseManager: {0}", inDesignMode);
}

private void Onload(object sender, EventArgs e)
{
    this.label3.Text = String.Format("OnLoadDesignMode: {0}", this.DesignMode);
}

I also created a Form with this Usercontrol on it.我还创建了一个带有此用户控件的表单。 If I use the designer to show the Form, I see the following:如果我使用设计器显示表单,我会看到以下内容: 在此处输入图像描述

Conclusion: in the constructor you can't use DesignMode.结论:在构造函数中你不能使用 DesignMode。 However,you can use the LicenseManager但是,您可以使用 LicenseManager

However, if I open the usercontrol in the designer, it seems like the constructor isn't even used!但是,如果我在设计器中打开用户控件,似乎甚至没有使用构造函数!

在此处输入图像描述

So now I am a bit confused.所以现在我有点困惑。

However, if I open the usercontrol in the designer, it seems like the constructor isn't even used.但是,如果我在设计器中打开用户控件,似乎甚至没有使用构造函数。 So now I am a bit confused.所以现在我有点困惑。

The designer doesn't call the constructor of the root component that you are designing.设计器不会调用您正在设计的根组件的构造函数。 Instead, it calls the constructor of the base class of the root component, then parse the designer-generated code of your root component, create child components and set properties and add them to the root and and load the designer.相反,它调用根组件的基础 class 的构造函数,然后解析根组件的设计器生成的代码,创建子组件并设置属性并将它们添加到根并加载设计器。

Now it should be clear what is happening here:现在应该清楚这里发生了什么:

  • When you are designing MyUserControl:UserControl , the constructor of UserControl will be called.在设计MyUserControl:UserControl时,会调用UserControl的构造函数。 (Constructor of MyUserControl is useless here.) MyUserControl的构造函数在这里没用。)

  • When you are designing Form1: Form which has an instance of MyUserControl on it, constructor of Form will be called, Form1.Designer.cs will be parsed and since there is an instance of MyUserControl , constructor of MyUserControl will run and an instance of the control will be added to design surface.当你设计Form1: Form上面有一个MyUserControl实例的 Form 时, Form的构造函数将被调用, Form1.Designer.cs将被解析,因为有一个MyUserControl的实例, MyUserControl的构造函数将运行并且一个实例控件将添加到设计表面。

You can find an interesting example in this post or the other which shows how designer parses and loads a form (which has some serious syntax problems in designer code).您可以在这篇文章其他文章中找到一个有趣的示例,它展示了设计器如何解析和加载表单(在设计器代码中存在一些严重的语法问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM