WPF关闭时执行代码

时间:2012-04-04 19:54:16

标签: wpf event-handling execute

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

我知道可以做到这一点,因为这个问题得到了回答:

Run code on WPF form close

但我需要一些方向。

谢谢=)

5 个答案:

答案 0 :(得分:35)

只是这个XAML

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

以及ClosingClosed事件的代码

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

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

答案 1 :(得分:23)

如果你想从后面的代码中完成所有操作,请将它放在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
        }
    }
}

如果你想参与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>

和.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
        }
    }
}

以上示例您可以应用于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);
            }
        }
    }
}

答案 2 :(得分:1)

你可以像这样覆盖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);
    }
}

答案 3 :(得分:1)

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

Josh Smith MVVM example

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

答案 4 :(得分:0)

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

在您的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...!
        }
    }
}

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

    ...
        private void InitializeComponent()
        {

            ...

            // 
            // Window
            // 
            ...

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

    ...