如何在WPF中使用Application.Exit事件?

Roc*_*etq 16 c# wpf events exit

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

此代码位于此处 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>
Run Code Online (Sandbox Code Playgroud)

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

我认为他无法找到这个功能.

Ofi*_*fir 46

这很简单:

将"Exit"属性添加到应用程序标记

<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>
Run Code Online (Sandbox Code Playgroud)

并在"代码背后"处理它

private void Application_Exit(object sender, ExitEventArgs e)
{
    // Perform tasks at application exit
}
Run Code Online (Sandbox Code Playgroud)

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


BRA*_*mel 13

你应该在你的xaml代码中添加app_exit

 <Application x:Class="CSharp.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>
Run Code Online (Sandbox Code Playgroud)

你可以挂钩事件关闭你的主窗口像这样 -

 <Window Closing="Window_Closing">
Run Code Online (Sandbox Code Playgroud)

在您的活动中,做您需要的所有工作

    private void Window_Closing(object sender, CancelEventArgs 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);
            }
}
Run Code Online (Sandbox Code Playgroud)

  • 我已经更新了答案:)希望对此有所帮助 (2认同)

Shl*_*nts 7

如果您不喜欢向 XAML 添加 Exit 事件,您可以尝试此替代方案。

将以下方法添加到您的App.xaml.cs

protected override void OnExit(ExitEventArgs e)
{
    base.OnExit(e);
    // Your code here
}
Run Code Online (Sandbox Code Playgroud)


小智 5

如果你想遵循 MVVM 原则,你可以使用 System.Windows.Interactivity.WPF。

主窗口.xaml

<Window x:Class="Endonext.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Closing">
        <i:InvokeCommandAction Command="{Binding WindowClosingCommand, Mode=OneTime}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)

MainWindowViewModel.cs

public class MainWindowViewModel 
{
    ICommand WindowClosingCommand => new RelayCommand(arg => this.WindowClosing());

    private void WindowClosing()
    {
      // do what you want.
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方法更具可测试性。祝你今天过得愉快。