5

如果您有一个显示数据的表单,您可以做的一件事是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();
        }
    }
 }

但是,如果您决定将该表单重写为 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();
        }
    }
}

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

4

1 回答 1

1

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

于 2012-07-09T23:13:15.983 回答