Application.Exit 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會在應用程式關閉且無法取消之前發生。
public:
event System::Windows::ExitEventHandler ^ Exit;
public event System.Windows.ExitEventHandler Exit;
member this.Exit : System.Windows.ExitEventHandler
Public Custom Event Exit As ExitEventHandler
事件類型
範例
下列範例示範如何:
Exit處理 事件。
檢查並更新 ApplicationExitCode 的 ExitEventArgs 屬性。
將專案寫入隔離儲存區中的應用程式記錄檔。
將應用程式狀態保存到隔離儲存區。
<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>
using System;
using System.Collections;
using System.Windows;
using System.IO;
using System.IO.IsolatedStorage;
namespace CSharp
{
public enum ApplicationExitCode
{
Success = 0,
Failure = 1,
CantWriteToApplicationLog = 2,
CantPersistApplicationState = 3
}
public partial class App : Application
{
void App_Exit(object sender, ExitEventArgs e)
{
try
{
// Write entry to application log
if (e.ApplicationExitCode == (int)ApplicationExitCode.Success)
{
WriteApplicationLogEntry("Failure", e.ApplicationExitCode);
}
else
{
WriteApplicationLogEntry("Success", e.ApplicationExitCode);
}
}
catch
{
// Update exit code to reflect failure to write to application log
e.ApplicationExitCode = (int)ApplicationExitCode.CantWriteToApplicationLog;
}
// Persist application state
try
{
PersistApplicationState();
}
catch
{
// Update exit code to reflect failure to persist application state
e.ApplicationExitCode = (int)ApplicationExitCode.CantPersistApplicationState;
}
}
void WriteApplicationLogEntry(string message, int exitCode)
{
// Write log entry to file in isolated storage for the user
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
using (Stream stream = new IsolatedStorageFileStream("log.txt", FileMode.Append, FileAccess.Write, store))
using (StreamWriter writer = new StreamWriter(stream))
{
string entry = string.Format("{0}: {1} - {2}", message, exitCode, DateTime.Now);
writer.WriteLine(entry);
}
}
void PersistApplicationState()
{
// Persist application state to file in isolated storage for the user
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
using (Stream stream = new IsolatedStorageFileStream("state.txt", FileMode.Create, store))
using (StreamWriter writer = new StreamWriter(stream))
{
foreach (DictionaryEntry entry in this.Properties)
{
writer.WriteLine(entry.Value);
}
}
}
}
}
Imports System.Collections
Imports System.Windows
Imports System.IO
Imports System.IO.IsolatedStorage
Namespace VisualBasic
Public Enum ApplicationExitCode
Success = 0
Failure = 1
CantWriteToApplicationLog = 2
CantPersistApplicationState = 3
End Enum
Partial Public Class App
Inherits Application
Private Sub App_Exit(ByVal sender As Object, ByVal e As ExitEventArgs)
Try
' Write entry to application log
If e.ApplicationExitCode = CInt(ApplicationExitCode.Success) Then
WriteApplicationLogEntry("Failure", e.ApplicationExitCode)
Else
WriteApplicationLogEntry("Success", e.ApplicationExitCode)
End If
Catch
' Update exit code to reflect failure to write to application log
e.ApplicationExitCode = CInt(ApplicationExitCode.CantWriteToApplicationLog)
End Try
' Persist application state
Try
PersistApplicationState()
Catch
' Update exit code to reflect failure to persist application state
e.ApplicationExitCode = CInt(ApplicationExitCode.CantPersistApplicationState)
End Try
End Sub
Private Sub WriteApplicationLogEntry(ByVal message As String, ByVal exitCode As Integer)
' Write log entry to file in isolated storage for the user
Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly()
Using stream As Stream = New IsolatedStorageFileStream("log.txt", FileMode.Append, FileAccess.Write, store)
Using writer As New StreamWriter(stream)
Dim entry As String = String.Format("{0}: {1} - {2}", message, exitCode, Date.Now)
writer.WriteLine(entry)
End Using
End Using
End Sub
Private Sub PersistApplicationState()
' Persist application state to file in isolated storage for the user
Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly()
Using stream As Stream = New IsolatedStorageFileStream("state.txt", FileMode.Create, store)
Using writer As New StreamWriter(stream)
For Each entry As DictionaryEntry In Me.Properties
writer.WriteLine(entry.Value)
Next entry
End Using
End Using
End Sub
End Class
End Namespace
備註
應用程式可能會因為下列其中一個原因而關閉:
會 Shutdown 明確呼叫 物件的 方法 Application ,或由 ShutdownMode 屬性所決定。
使用者藉由登出或關閉來結束會話。
您可以藉由處理 Exit 事件來偵測應用程式關機的時間,並視需要執行任何其他處理。
當您不需要明確呼叫 Shutdown 時,您也可以處理 Exit 以檢查或變更應用程式結束代碼。 結束代碼會從 ApplicationExitCode 傳遞至 Exit 事件處理常式之引數的 ExitEventArgs 屬性公開。 當應用程式停止執行時,結束代碼會傳遞至作業系統以供後續處理。
如果您的應用程式處理 SessionEnding 事件並後續取消事件,則不會引發 , Exit 而且應用程式會根據關機模式繼續執行。
您可以從 XAML 瀏覽器應用程式設定結束代碼, (XBAP) ,不過會忽略此值。
針對 XBAP, Exit 會在下列情況下引發:
XBAP 會從中巡覽。
在 Internet Explorer 7 中,當裝載 XBAP 的索引標籤關閉時。
關閉瀏覽器時。
在所有情況下,都會忽略 屬性的值 ApplicationExitCode 。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應