Execute code when a WPF closes

早过忘川 提交于 2019-11-27 21:16:42

It's just this XAML

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

and code for both the Closing and Closed events

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

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

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

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

<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

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

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

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

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

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 example

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

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

In your Window.cs file

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

    ...
        private void InitializeComponent()
        {

            ...

            // 
            // Window
            // 
            ...

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

    ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!