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

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

问题描述

我需要删除一些特定的文件,那么用户在WPF关闭程序。所以,我想MDSN code从这里<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.application.exit.aspx\">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:

这code设在这里 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>

一切不删档的第一,其次该程序保持在这个过程后,我推退出按钮(这真的很奇怪)。这code不给任何错误。最后它不显示的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>

和处理它的身后code

and handle it in the "code behind"

private void Application_Exit(object sender, ExitEventArgs e)
{
    // Perform tasks at application 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.

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

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