[英]How to use Application.Exit Event in WPF?
我需要删除某些文件,然后用户在 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>
首先它不删除文件,其次这个程序在我按下退出按钮后停留在进程中(这真的很奇怪)。 此代码不会给出任何错误。 最后它不显示MessageBox
所以这里有什么问题吗?
我想他就是找不到这个功能。
这很简单:
将“退出”属性添加到应用程序标签
<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 事件之后触发。 您无法取消退出事件。
你应该在你的 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);
}
}
如果您不喜欢将 Exit 事件添加到 XAML,您可以尝试这种替代方法。
将以下方法添加到您的App.xaml.cs
:
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
// Your code here
}
如果你想遵循 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>
主窗口视图模型.cs
public class MainWindowViewModel
{
ICommand WindowClosingCommand => new RelayCommand(arg => this.WindowClosing());
private void WindowClosing()
{
// do what you want.
}
}
这种方法更具可测试性。 有一个美好的一天。
<Application></Application>
属性下,双击“退出”旁边的文本框。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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.