如何判断 WPF 窗口是否已关闭? [英] How do you tell if a WPF Window is closed?

查看:98
本文介绍了如何判断 WPF 窗口是否已关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序显示一些可以由用户关闭或自动关闭的子窗口.在调试一些抛出的异常时,我发现我试图在已经关闭的窗口上调用诸如 Hide() 之类的方法;这个特定的代码分支在两种情况下都是通用的,我没有注意到这一点.

I'm working on an application that displays some child windows which can either be closed by the user or are automatically closed. While debugging some exceptions that were being thrown, I discovered I was trying to call methods like Hide() on a window that had already been closed; this particular branch of code was common to both cases and I hadn't noticed this.

我的第一个想法是在 Window 上查找一个属性,该属性表示窗口已关闭.我似乎找不到一个.在 WinForms 中,我会查看 IsDisposed 属性以获得某种可靠的指示,表明表单已关闭(它不能可靠地用于对话框,但我不使用对话框.)我在 Window 上看不到任何等效的东西.Window.Close() 的文档似乎没有指出任何由该方法更改的属性.我是否遗漏了一些明显的东西,或者是知道窗口是否已关闭以处理 Closed 事件的唯一方法?对于一项简单的任务来说,这似乎是一项苛刻的要求.

One of my first ideas was to look for a property on Window that would indicate the window had been closed. I can't seem to find one. In WinForms, I'd look to the IsDisposed property for a somewhat reliable indicator that the form had been closed (it won't reliably work for a dialog but I'm not working with dialogs.) I don't see anything equivalent on Window. The documentation for Window.Close() doesn't seem to indicate any properties that are changed by the method. Am I missing something obvious, or is the only method to know if a window's been closed to handle the Closed event? That seems kind of a harsh requirement for a simple task.

推荐答案

根据 这个对话在MSDN WPF论坛上(见上一篇),你可以检查IsLoaded是否为false,这意味着窗口是合格的"" 用于卸载其内容.我希望这对你有用!

According to this conversation on the MSDN WPF forums (see the last post), you can check to see if the IsLoaded is false, which means that the window is "eligible" for unloading its content. I hope that works for you!

这篇关于如何判断 WPF 窗口是否已关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆