如何取消从页面关闭WPF应用程序?
我正在使用 WPF NavigationWindow 和一些页面,并且希望能够知道应用程序何时从页面关闭。
如何在不通过 NavigationWindow_Closing 事件处理程序主动通知页面的情况下完成此操作?
我知道此处的技术,但不幸的是 NavigationService_Navigating 不是应用程序关闭时不会调用。
I'm using a WPF NavigationWindow and some Pages and would like to be able to know when the application is closing from a Page.
How can this be done without actively notifying the page from the NavigationWindow_Closing event handler?
I'm aware of the technique here, but unfortunately NavigationService_Navigating isn't called when the application is closed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您的问题是如何访问
NavigationWindow
内托管的内容。知道窗口本身正在关闭是很简单的,例如您可以订阅Closing
事件。要获取
NavigationWindow
中托管的Page
,您可以使用VisualTreeHelper
向下钻取其后代,直到找到唯一的WebBrowser
控件。您可以手动编码,但是很好像这样的代码可以在网上使用。获得
WebBrowser
后,使用WebBrowser.Document
属性。If I understand you correctly, your problem is how to get access to the content hosted inside the
NavigationWindow
. Knowing that the window itself is closing is trivial, e.g. there is theClosing
event you can subscribe to.To get the
Page
hosted in theNavigationWindow
, you can useVisualTreeHelper
to drill down into its descendants until you find the one and onlyWebBrowser
control. You could code this manually, but there is good code like this ready to use on the net.After you get the
WebBrowser
, it's trivially easy to get at the content with theWebBrowser.Document
property.实现此目的的一种方法是让所涉及的页面支持一个接口,例如:
在页面级别实现此接口:
在导航窗口中,检查子级是否属于 ICanClose:
One way to do this is have the pages involved support an interface such as:
Implement this interface at the page level:
In the navigation window, check to see if the child is of ICanClose: