在WinForms UserControl构造函数中是否需要更换DesignMode for LicenseManager.UsageMode?

Aar*_*ide 5 c# user-controls winforms

如果您有一个显示数据的表单,您可以做的一件事是this.DesignMode在构造函数中引用以避免在设计器中填充它:

public partial class SetupForm : Form
{
    private SetupItemContainer container = new SetupItemContainer();

    public SetupForm()
    {
        InitializeComponent();
        if (!this.DesignMode)
        {
            this.bindingSource1.DataSource = this.container;
            this.Fill();
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

但是,如果您决定将该表单重新编写为UserControl,保持相同的构造函数逻辑,this.DesignMode则会出现意外情况 - 无论如何都是假的.这导致设计者调用您的逻辑,这些逻辑意味着在运行时发生.

我刚刚在博客文章中发现了一条评论,似乎对此进行了修复,但它引用了LicenseManager类的功能,作为在UserControl中按预期工作的替代品.

所以对于UserControl,我可以这样做:

public partial class AffiliateSetup : UserControl
{
    private AffiliateItemContainer container = new AffiliateItemContainer();

    public AffiliateSetup()
    {
        InitializeComponent();
        if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
        {
            this.bindingSource1.DataSource = this.container;
            this.Fill();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用LicenseManager而不是DesignMode有任何警告或暗示可能会阻止我放入我的生产代码?

adr*_*nks 1

根据对我对另一个问题的回答发表评论的人的说法,使用LicenseManager在方法中不起作用OnPaint