处理 WPF 窗口的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/568408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct way to dispose of a WPF window?
提问by vanja.
I have a WPF window which I am creating from another window by calling Show()
, then letting it Close()
itself. When the window closes, I expect it to die, call its destructor, and delete all its child elements (such as timers..).
我有一个 WPF 窗口,我通过调用从另一个窗口创建Show()
它,然后让它Close()
自己创建。当窗口关闭时,我希望它死掉,调用它的析构函数,并删除它的所有子元素(例如计时器......)。
What is the correct way of invoking such an action?
调用此类操作的正确方法是什么?
回答by Simon Buchan
Close()
releases all unmanaged resources, and closes all owned Window
s.
Close()
释放所有非托管资源,并关闭所有拥有Window
的资源。
Any other managed resources you need deterministic disposal of should be handled from the Closedevent.
您需要确定性处置的任何其他托管资源都应从Closed事件处理。
(note: deleted previous answer, it was a completely wrong guess)
(注意:删除了以前的答案,这是一个完全错误的猜测)
回答by Samuel Hyman
There are very few WPF elements that actually need to be explicitly disposed, unlike in Windows Forms.
与 Windows 窗体不同,实际上需要显式处理的 WPF 元素很少。
In the case of Window, calling Close() is sufficient to dispose all managed and unmanaged resources accorrding to the documentation.
在 Window 的情况下,调用 Close() 足以根据文档处置所有托管和非托管资源。
回答by rookie1024
Just in case, I'll add my two cents.
以防万一,我会加上我的两分钱。
My problem was that I didn't do enough troubleshooting. My window was a child window that could be opened, closed, and re-opened, so I added the following to keep it from closing completely:
我的问题是我没有做足够的故障排除。我的窗口是一个可以打开、关闭和重新打开的子窗口,因此我添加了以下内容以防止它完全关闭:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
e.Cancel = true;
this.Hide();
}
However, when Window.Close was called, it only hid the window. I eventually caught on and added the following:
然而,当 Window.Close 被调用时,它只隐藏了窗口。我最终理解并添加了以下内容:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
e.Cancel = true;
this.Hide();
}
public void Close() {
this.Closing -= Window_Closing;
//Add closing logic here.
base.Close();
}
This works fine - it removes the event handler preventing the window from being closed, and then closes it.
这工作正常 - 它删除了阻止窗口关闭的事件处理程序,然后关闭它。
回答by Drew Noakes
Closing the window and being confident that you have released all resources to it and any of its children will cause all well behaved elements in the logic tree to be garbage collected.
关闭窗口并确信您已将所有资源释放给它及其任何子项将导致逻辑树中所有行为良好的元素被垃圾收集。
I say "well behaved" because it's theoretically possible to have an element that does something like create a thread that isn't stopped properly, but in practice if you're using the basic WPF framework and well written controls, you should be fine to just release everything.
我说“表现良好”是因为理论上可能有一个元素可以创建一个没有正确停止的线程,但实际上如果你使用基本的 WPF 框架和编写良好的控件,你应该没问题只是释放一切。