.net – 重新创建UserControl句柄的频率是多少?

我在msdn上查找了 winforms(找到 here)的UserControl.Load事件并看到了这条警告消息:

The Load event occurs when the handle for the UserControl is created.
In some circumstances, this can cause the Load event to occur more
than one time. For example, the Load event occurs when the UserControl
is loaded, and again if the handle is recreated. (One way that a
handle is recreated is by calling the RecreateHandle method.) To
account for the Load event occurring more than one time, you should
put any one time initialization code in the UserControl constructor
instead of a Load event handler. In addition, you should not add data
bindings to the UserControl in a Load event handler.

那么我的问题是,除了对RecreateHandle的显式调用之外,还会导致为UserControl重新创建句柄吗?在阅读本文之前,我总是把任何只应该在Load事件中发生过一次的事情.另外(从最后一句),哪里是添加数据绑定的最佳位置?如果我不打电话给RecreateHandle真的很重要吗?

是的,这是可能的.它是由本机Windows CreateWindowEx()API函数的问题引起的.该调用指定窗口的样式位.这些相同的样式位也作为Control的属性公开.问题是,更改该属性需要再次调用CreateWindowEx().控件的本机Windows窗口将被销毁并重新创建.这有副作用,其中一个是让Load事件再次运行.

使用代码示例演示它:

public partial class UserControl1 : UserControl {
    public UserControl1() {
        InitializeComponent();
    }
    public void TriggerRecreate() {
        if (this.RightToLeft == System.Windows.Forms.RightToLeft.Yes)
             this.RightToLeft = System.Windows.Forms.RightToLeft.No;
        else this.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
    }
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        Console.WriteLine("onload");
    }
}

在窗体上编译并删除控件.添加一个按钮:

private void button1_Click(object sender, EventArgs e) {
        userControl11.TriggerRecreate();
    }

并观察每次单击按钮时“输出”窗口显示“onload”.

对于将执行此操作的UserControl,RightToLeft属性是我能想到的唯一属性.表格还有很多.然而,该类具有专用代码来防止其OnLoad方法多次运行.不知道为什么他们没有为UserControl做这个,可能是因为它是如此罕见.随意忽略它.并且总是喜欢构造函数而不是Load事件,除非你关心窗口大小.

相关文章
相关标签/搜索