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

问题描述 投票:16回答:5

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

此代码位于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那么这里有什么问题吗?

我认为他只是找不到此功能。

c# wpf events exit
5个回答
45
投票

非常简单:

向应用程序标签添加“退出”属性

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

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


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>

您可以像这样在主窗口上挂起事件Closing-

 <Window Closing="Window_Closing">

并且在您要完成所有需要的工作的情况下

    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);
            }
}

1
投票

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

MainWindow.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>

MainWindowViewModel.cs

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

    private void WindowClosing()
    {
      // do what you want.
    }
}

此方法更具可测试性。祝你有美好的一天。


0
投票
  1. 确保名称空间(MyApp)匹配“ x:Class = MyApp ...”
  2. <Application></Application>的属性下,双击“退出”旁边的文本框。
  3. 如果出现“无法添加事件处理程序”,则重建项目会为我修复它。

XAML

<Application x:Class="MyApp.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"
>

后面的代码

using System.Windows;

namespace MyApp
{ 
   public partial class App : Application
   {
     private void Application_Exit(object sender, ExitEventArgs e)
     {
         //your code
     }
  }
}

0
投票

如果您不喜欢将退出事件添加到XAML,则可以尝试这种替代方法。

App.xaml.cs中添加以下方法:

protected override void OnExit(ExitEventArgs e)
{
    base.OnExit(e);
    // Your code here
}
© www.soinside.com 2019 - 2024. All rights reserved.