如何在 WPF 中使用 Application.Exit 事件? [英] How to use Application.Exit Event in WPF?

查看:31
本文介绍了如何在 WPF 中使用 Application.Exit 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除某些文件,然后用户在 WPF 中关闭程序.所以我从这里尝试了 MDSN 代码 http://msdn.microsoft.com/en-us/library/system.windows.application.exit.aspx 这样:

I need to delete some certain files, then user closes program in WPF. So I tried MDSN code from here http://msdn.microsoft.com/en-us/library/system.windows.application.exit.aspx this way:

此代码位于此处 App.xml.cs

public partial class App : Application
{
 void App_Exit(object sender, ExitEventArgs e)
    {
       MessageBox.Show("File deleted");
        var systemPath = System.Environment.GetFolderPath(
                                  Environment.SpecialFolder.CommonApplicationData);

                var _directoryName1 = Path.Combine(systemPath, "RadiolocationQ");
                var temp_file = Path.Combine(_directoryName1, "temp.ini");

                if (File.Exists(temp1_file))
                {
                    File.Delete(temp1_file);
                }

    }

}

// App.xaml
<Application x:Class="ModernUIApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             ShutdownMode="OnExplicitShutdown"
             Exit="App_Exit">
    <Application.Resources>

首先它不删除文件,其次这个程序在我按下退出按钮后停留在进程中(这真的很奇怪).此代码不会给出任何错误.最后它没有显示 MessageBox 那么这里有什么问题吗?

First of all it doesn't delete files, secondly this program stays in the process after I pushed exit button( this is really strange). This code don't give any errors. And finally it doesn't show MessageBox So anything wrong here?

我觉得他就是找不到这个功能.

I think he just can`t find this function.

推荐答案

很简单:

为应用程序标签添加退出"属性

Add "Exit" property to the application tag

<Application x:Class="WpfApplication4.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             Exit="Application_Exit">
</Application>

并在代码隐藏"中处理

private void Application_Exit(object sender, ExitEventArgs e)
{
    // Perform tasks at application exit
}

Exit 事件在应用程序关闭或 Windows 会话结束时触发.它在 SessionEnding 事件之后触发.您无法取消退出事件.

The Exit event is fired when the application is shutting down or the Windows session is ending. It is fired after the SessionEnding event. You cannot cancel the Exit event.

这篇关于如何在 WPF 中使用 Application.Exit 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆