WPF的退出

很多时候,会自己写退出程序的代码。

比如,先显示登录框(LogIn),成功后隐藏它,并显示一个主窗体(MainWin),或者外部还调用了其他App,当你关闭MainWin不一定会直接退出整个程序的。

我们可以直接终止相关进程:

  System.Environment.Exit(0);

当然,你可能还想在退出前做一些什么,比如保存一下缓存数据,清空临时文件,等等操作,就需要这样,

在项目的 App 条目下增加关闭逻辑:(App.xaml.cs)

    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            this.Exit += App_Exit;
        }

        void App_Exit(object sender, ExitEventArgs e)
        {
            //关闭前的一些交互逻辑,如保存

            System.Environment.Exit(0);

            //throw new NotImplementedException();
        }
.........

 类似的还有这个:

 System.Diagnostics.Process.GetCurrentProcess().Kill();
原文地址:https://www.cnblogs.com/3Tai/p/3793052.html