我正在使用 WPF NavigationWindow 和一些页面,并且希望能够知道应用程序何时从页面关闭。
如果不从 NavigationWindow_Closing 事件处理程序主动通知页面,如何做到这一点?
我知道这里的技术,但不幸的是,在应用程序关闭时没有调用 NavigationService_Navigating。
我正在使用 WPF NavigationWindow 和一些页面,并且希望能够知道应用程序何时从页面关闭。
如果不从 NavigationWindow_Closing 事件处理程序主动通知页面,如何做到这一点?
我知道这里的技术,但不幸的是,在应用程序关闭时没有调用 NavigationService_Navigating。
如果我理解正确,您的问题是如何访问托管在NavigationWindow
. 知道窗口本身正在关闭是微不足道的,例如,Closing
您可以订阅某个事件。
要在 中获得Page
托管NavigationWindow
,您可以使用VisualTreeHelper
向下钻取其后代,直到找到唯一的WebBrowser
控件。您可以手动编写代码,但是网上有类似这样的好代码可供使用。
获得 后WebBrowser
,就很容易获得该WebBrowser.Document
属性的内容。
一种方法是让所涉及的页面支持一个接口,例如:
public interface ICanClose
{
bool CanClose();
}
在页面级别实现此接口:
public partial class Page1 : Page, ICanClose
{
public Page1()
{
InitializeComponent();
}
public bool CanClose()
{
return false;
}
}
在导航窗口中,检查孩子是否属于 ICanClose:
private void NavigationWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
ICanClose canClose = this.Content as ICanClose;
if (canClose != null && !canClose.CanClose())
e.Cancel = true;
}