【发布时间】: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