在 WPF 关闭时执行代码

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

Execute code when a WPF closes

wpfevent-handlingexecute

提问by Stylzs05

I am not familiar with using event handlers, and I was wondering if anyone had or could direct me to some code that shows how to use an event handler that will execute code on the Close/Closed event?

我不熟悉使用事件处理程序,我想知道是否有人拥有或可以将我引导到一些代码,该代码显示如何使用将在 Close/Closed 事件上执行代码的事件处理程序?

I know this can be done because of this answered question:

我知道这是可以做到的,因为这个回答的问题:

Run code on WPF form close

在 WPF 表单上运行代码关闭

But I need some direction.

但我需要一些指导。

Thank you =)

谢谢你=)

回答by Clemens

It's just this XAML

这只是这个 XAML

<Window ... Closing="Window_Closing" Closed="Window_Closed">
    ...
</Window>

and code for both the Closingand Closedevents

ClosingClosed事件的代码

private void Window_Closing(object sender, CancelEventArgs e)
{
    ...
}

private void Window_Closed(object sender, EventArgs e)
{
    ....
}

回答by Dan P

If you want to do it all from code behind put this in your windows .cs file

如果你想从背后的代码中完成这一切,把它放在你的 windows .cs 文件中

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Closed += new EventHandler(MainWindow_Closed);
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            //Put your close code here
        }
    }
}

If you want to do part in xaml and part in code behind do this in xaml

如果您想参与 xaml 并参与后面的代码,请在 xaml 中执行此操作

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Closed="MainWindow_Closed">
    <Grid>

    </Grid>
</Window>

and this in .cs

这在 .cs 中

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            //Put your close code here
        }
    }
}

The above to examples you can apply to any form in a xaml app. You can have multiple forms. If you want to apply code for the entire application exit process modify your app.xaml.cs file to this

以上示例可以应用于 xaml 应用程序中的任何表单。您可以有多种表格。如果要为整个应用程序退出过程应用代码,请将 app.xaml.cs 文件修改为此

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnExit(ExitEventArgs e)
        {
            try
            {
                //Put your special code here
            }
            finally
            {
                base.OnExit(e);
            }
        }
    }
}

回答by Dmitry Reznik

You can override the OnExit function in App.Xaml.cs like this:

您可以像这样覆盖 App.Xaml.cs 中的 OnExit 函数:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    protected override void OnExit(ExitEventArgs e)
    {
        //do your things
        base.OnExit(e);
    }
}

回答by Jimmy Lyke

Josh Smith's article on MVVM has a nice example of ViewModels that are part of a workspace and what to do on close. This architecture can be expanded beyond just your window being closed, but cleaning up ViewModels, etc.

Josh Smith 关于 MVVM 的文章有一个很好的示例,展示了作为工作区一部分的 ViewModel 以及关闭时要做什么。这种架构可以扩展到不仅仅是关闭窗口,还可以清理 ViewModels 等。

Josh Smith MVVM example

Josh Smith MVVM 示例

In Figure 7 he describes the situation you are talking about. Hope this helps!

在图 7 中,他描述了您正在谈论的情况。希望这可以帮助!

回答by yogurt1989

If you are using C# on Microsoft Visual Studio, the following worked for me.

如果您在 Microsoft Visual Studio 上使用 C#,以下内容对我有用。

In your Window.cs file

在您的 Window.cs 文件中

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Name_Space
{
    public partial class Window : Form
    {

        public Window()
        {
            InitializeComponent();
           //...
        }

        private void Window_Load(object sender, EventArgs e)
        {
          //...
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            // Your code goes here...!
        }
    }
}

In your Window.Designer.cs file add this line to the following method

在您的 Window.Designer.cs 文件中,将此行添加到以下方法中

    ...
        private void InitializeComponent()
        {

            ...

            // 
            // Window
            // 
            ...

            this.Closed += new System.EventHandler(this.Window_Closed); // <-- add this line
         }

    ...