27

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 =)

4

4 に答える 4

54

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, CancelEventArgs e)
{
    ...
}

private void Window_Closed(object sender, EventArgs e)
{
    ....
}
于 2012-04-04T20:01:26.790 に答える
27

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-04T20:02:17.607 に答える
3

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-04T19:59:39.680 に答える
-2

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-13T23:14:43.967 に答える