【问题标题】:Are there any caveats to swapping out DesignMode for LicenseManager.UsageMode in a WinForms UserControl constructor?在 WinForms UserControl 构造函数中将 DesignMode 换成 LicenseManager.UsageMode 是否有任何注意事项?
【发布时间】:2012-07-09 09:42:30
【问题描述】:

如果您有一个显示数据的表单,您可以做的一件事是在构造函数中引用 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 是否有任何警告或暗示可能会阻止我放入生产代码?

【问题讨论】:

    标签: c# winforms user-controls


    【解决方案1】:

    根据在my answer to another question 上发表评论的人所说,使用LicenseManagerOnPaint 方法中不起作用。

    【讨论】:

    • 这很模糊。听起来它在OnPaint 中几乎不起作用,因为“循环引用”(如果许可模型不正确,也许OnPaint 首先不会触发?)
    • @RobertHarvey 我同意,这很模糊。我尚未对其进行测试,因此无法确认是否属实,但该评论有两个赞成票,因此我认为至少有两个人遇到了这种行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    相关资源
    最近更新 更多