如何取消从页面关闭WPF应用程序?

发布于 2024-10-30 14:49:11 字数 289 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2024-11-06 14:49:11

如果我理解正确,您的问题是如何访问 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 the Closing event you can subscribe to.

To get the Page hosted in the NavigationWindow, you can use VisualTreeHelper to drill down into its descendants until you find the one and only WebBrowser 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 the WebBrowser.Document property.

不美如何 2024-11-06 14:49:11

实现此目的的一种方法是让所涉及的页面支持一个接口,例如:

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;
}

One way to do this is have the pages involved support an interface such as:

public interface ICanClose
{
     bool CanClose();
}

Implement this interface at the page level:

public partial class Page1 : Page,  ICanClose
{
    public Page1()
    {
        InitializeComponent();
    }

    public bool CanClose()
    {
        return false;
    }
}

In the navigation window, check to see if the child is of 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文