问题描述
这是关于 MDI 设置的 .net WinForm 问题.
This is .net WinForm question about MDI setting.
当主窗体创建 MDI 子窗体时,主窗体的 PropertyStore
保存对 MDI 子窗体的引用.我想知道这是否会导致子窗体即使关闭也能存活.如果是这样,在处理子表单时我该怎么做才能删除此引用?
When the main form creates an MDI child form, the main form's PropertyStore
holds a reference to the MDI child form. I wonder whether this will cause the child form to be alive even if it is closed. If so, what shall I do when disposing the child form in order to remove this reference?
子窗体通过示例代码调用:
The child form is called by sample code:
//The code is in the main form.
var f = new FormMDIChild();
f.MdiParent = this;
f.Show();
推荐答案
作为记录,引用的帖子中提供的解决方案确实有效(尽管有点冒险).但是,如果您打开和关闭另一个子窗体,泄漏也会消失,看来 MDI 父级只会泄漏最后一个打开的子窗体.
For the record, the solution offered in the referenced post does work (though it's a little dicey). However, the leak also goes away if you open and close another child form, it appears that the MDI Parent only leaks the last opened child.
如果您想使用参考帖子中提到的解决方法来修复泄漏,只需覆盖 MDIParent 的 OnMdiChildActivate 方法...
If you want to fix the leak by using the work around mentioned in the referenced post, just override the MDIParent's OnMdiChildActivate method...
protected override void OnMdiChildActivate(EventArgs e)
{
base.OnMdiChildActivate(e);
typeof(Form).InvokeMember("FormerlyActiveMdiChild",
BindingFlags.Instance | BindingFlags.SetProperty |
BindingFlags.NonPublic, null, this, new object[] { null });
}
这篇关于关于 PropertyStore 和 MDI 子窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!