Execute code when a WPF closes

Go To StackoverFlow.com

14

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?

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

Run code on WPF form close

But I need some direction.

Thank you =)

2012-04-04 19:54
by Stylzs05
Look here and use OnExit instead of OnStartup. http://manaspatnaik.com/blog/index.php/technology/wpf/event-handling-in-wpf/19 - mydogisbox 2012-04-04 19:59


33

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)
{
    ....
}
2012-04-04 20:01
by Clemens


20

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);
            }
        }
    }
}
2012-04-04 20:02
by Dan P


1

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);
    }
}
2012-04-04 19:59
by Dmitry Reznik
The "OnExit()" method is not showing up in the intellisense and I know I have the correct using statement because MSDN says it's in the System.Windows namespace. Any ideas - Stylzs05 2012-04-04 20:26
Excactly, what I needed. @Stylzs05: It is an old question, but for those who come here: intellisense is kicking in, if you write protected override to declare the method - FrankM 2018-10-17 09:18


1

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!

2012-04-04 20:01
by Jimmy Lyke


0

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
         }

    ...
2018-09-13 23:14
by yogurt1989
Ads