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 传递给事件处理程序的参数的 ExitEventArgs 属性公开 Exit 。 当应用程序停止运行时,退出代码将传递给操作系统以供后续处理。
如果应用程序处理事件 SessionEnding 并随后取消它,则不会引发 , Exit 并且应用程序继续按照关闭模式运行。
可以从 XAML 浏览器应用程序 (XBAP) 设置退出代码,但该值将被忽略。
对于 XBAP, Exit 在以下情况下引发:
- 导航离开 XBAP。
- 当托管 XBAP 的浏览器选项卡关闭时。
- 关闭浏览器时。
在所有情况下,都会忽略 属性的值 ApplicationExitCode 。
有关 XBAP 支持的详细信息,请参阅 有关 WPF 浏览器托管应用程序的常见问题 (XBAP) 。