wpf 页面关闭事件或预览已卸载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12249496/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:11:54  来源:igfitidea点击:

Page Closing Event Or Preview Unloaded?

c#wpfxaml

提问by RobHurd

Firstly, thank you for taking the time out to read this post.

首先,感谢您抽出宝贵时间阅读这篇文章。

Let me get straight to my questions.

让我直接回答我的问题。

Is there any way to catch if a WPF page is closing before it is closed? i.e. some kind of PreviewClosing event? I have noticed that there is an Unloaded event, but I need something to trigger before the page is closed.

有什么方法可以在 WPF 页面关闭之前捕获它吗?即某种 PreviewClosing 事件?我注意到有一个 Unloaded 事件,但我需要在页面关闭之前触发一些东西。

My applications has a Window. The main body of the Windowcontains a Frame, the frame is then used to navigate to a Page.

我的应用程序有一个WindowWindow 的主体包含一个Frame,然后使用该框架导航到一个Page

WPF Window XAML

WPF 窗口 XAML

<Grid>
    <Frame x:Name="_mainFrame" />
</Grid>

C# Navigates my Window Frame to a Page named SpecEditor

C# 将我的窗口框架导航到名为 SpecEditor 的页面

_mainFrame.Navigate(new SpecEditor(customerDetails));

I need some way to trigger an event in the SpecEditor Pagewhen the hosting Window is trying to close (i.e. a PreviewClosing event), so that I can perfrom various checks. Mainly to prompt the user to save any unsaved work.

当托管窗口尝试关闭时(即 PreviewClosing 事件),我需要某种方式来触发SpecEditor 页面中的事件,以便我可以执行各种检查。主要是提示用户保存任何未保存的工作。

Any ideas how I can get around this as the page doesn't seem to have any PreviewClosing or PreviewUnloaded events.

我如何解决这个问题的任何想法,因为页面似乎没有任何 PreviewClosing 或 PreviewUnloaded 事件。

Thank you again for your time.

再次感谢您的时间。

****EDIT ******

** **编辑*** ***

Ok I've managed to figure out what I need.

好的,我已经设法弄清楚我需要什么。

In the main window I assign the PreviewClosing event:

在主窗口中,我分配了 PreviewClosing 事件:

private void RadWindow_PreviewClosed(object sender, WindowPreviewClosedEventArgs e)
    {
    }

To access the "Frame" in the main window you can use the following (note I named my frame to _mainFrame):

要访问主窗口中的“框架”,您可以使用以下命令(注意我将框架命名为 _mainFrame):

this._mainFrame.Content

In my case, the page which is hosted in the Frame is named "SpecEditor" which contains a docking control. So to access the content of my page I can do the following:

就我而言,框架中托管的页面名为“SpecEditor”,其中包含一个停靠控件。因此,要访问我的页面的内容,我可以执行以下操作:

((SpecEditor)this._mainFrame.Content).radDocking

It casts the content of my frame (which is the page) to my custom page class (SpecEditor) which allows me to access it's content, such as my Docking control.

它将我的框架(即页面)的内容转换为我的自定义页面类 (SpecEditor),这允许我访问它的内容,例如我的 Docking 控件。

For good coding it is worth checking that the frame content is actually the page you're trying to cast to. You can do this like so:

为了进行良好的编码,值得检查框架内容是否实际上是您尝试投射到的页面。你可以这样做:

if (this._mainFrame.Content is SpecEditor)
    {
      ((SpecEditor)this._mainFrame.Content).radDocking
      .... etc etc
    }

I hope this helps.

我希望这有帮助。

回答by ergohack

If you are really using frames, this post shows an alternate approach: https://highfieldtales.wordpress.com/2013/07/27/how-to-prevent-the-navigation-off-a-page-in-wpf/

如果你真的在使用框架,这篇文章展示了另一种方法:https: //highfieldtales.wordpress.com/2013/07/27/how-to-prevent-the-navigation-off-a-page-in-wpf/

The author uses the events for the frame's navigation facility.

作者将事件用于框架的导航工具。