Svi*_*ish 7 c# events user-controls winforms
你在哪里取消订阅UserControl?我在Load活动中订阅了它,就像我在表格中所做的那样.在表格中,我通常会在Closing活动中取消订阅,但我在UserControl...中找不到类似的东西.
有时您会想要这样做(例如,当使用CAB时)。
为了完整起见,您的问题的答案是:
protected override void OnCreateControl()
{
base.OnCreateControl();
if(!DesignMode) //only time this.ParentForm should be null
this.ParentForm.FormClosing += ParentForm_FormClosing;
}
private void ParentForm_FormClosing(object sender, FormClosingEventArgs e)
{
//Unregister events here
}
Run Code Online (Sandbox Code Playgroud)
你也可以覆盖Dispose()
有必要取消订阅吗?卸载后是否保留对用户控件的引用?如果没有,您不需要担心事件处理程序,因为一旦用户控件从内存中删除,事件处理程序也会被删除。你不会这样\xe2\x80\x99t 泄漏引用。
\n| 归档时间: |
|
| 查看次数: |
3290 次 |
| 最近记录: |