[英]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.