WindowsXamlManager.XamlShutdownCompletedOnThread 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生於 XAML 運行時間在目前線程上完成其關機程式時。
// Register
event_token XamlShutdownCompletedOnThread(TypedEventHandler<WindowsXamlManager, XamlShutdownCompletedOnThreadEventArgs const&> const& handler) const;
// Revoke with event_token
void XamlShutdownCompletedOnThread(event_token const* cookie) const;
// Revoke with event_revoker
WindowsXamlManager::XamlShutdownCompletedOnThread_revoker XamlShutdownCompletedOnThread(auto_revoke_t, TypedEventHandler<WindowsXamlManager, XamlShutdownCompletedOnThreadEventArgs const&> const& handler) const;
public event TypedEventHandler<WindowsXamlManager,XamlShutdownCompletedOnThreadEventArgs> XamlShutdownCompletedOnThread;
function onXamlShutdownCompletedOnThread(eventArgs) { /* Your code */ }
windowsXamlManager.addEventListener("xamlshutdowncompletedonthread", onXamlShutdownCompletedOnThread);
windowsXamlManager.removeEventListener("xamlshutdowncompletedonthread", onXamlShutdownCompletedOnThread);
- or -
windowsXamlManager.onxamlshutdowncompletedonthread = onXamlShutdownCompletedOnThread;
Public Custom Event XamlShutdownCompletedOnThread As TypedEventHandler(Of WindowsXamlManager, XamlShutdownCompletedOnThreadEventArgs)
事件類型
範例
此範例示範當 XAML 不再在線程上執行時,如何訂閱 事件來清除物件。
在此範例中,您會有一些想要在應用程式關閉時清除的不同物件類型。 XAML 有一些這些物件的間接參考 (例如 Model-View-ViewModel 應用程式中的「模型」物件) ,因此在 XAML 完成其工作之前,您不想要終結這些物件。 您也有一些物件會在另一個進程中清除,而且您也想要等候這些物件。
因此,您會使用 XamlShutdownCompletedOnThread 事件及其延遲來組織關機程式。 引發此事件時,您知道 XAML 是使用此線程上的 物件來完成。 您也可以採取延遲,以便在遠端作業完成之前,關機不會完成。
WindowsXamlManager manager = WindowsXamlManager::GetForCurrentThread();
manager.XamlShutdownCompletedOnThread([](
const WindowsXamlManager& sender,
const XamlShutdownCompletedOnThreadEventArgs& args) -> IAsyncAction
{
// Once we get this deferral, the DispatcherQueue shutdown process
// won't continue until Complete is called.
// Until we call deferral.Complete(), we can still use the
// DispatcherQueue and give it new work.
auto deferral = args.GetDispatcherQueueDeferral();
// Now that XAML has shutdown, we can clean up any of our objects
// that XAML might have been using.
CleanupUIThreadObjects();
// Capture the UI thread context.
winrt::apartment_context ui_thread;
// We can also do cleanup work that might take a while. For example,
// we can wait for work in other processes to finish.
co_await CleanupRemoteObjects();
// Switch back to the UI thread, in case we have any UI-thread work left to do.
// It will still be running because we took the deferral.
co_await ui_thread;
// Done! Shutdown may continue.
deferral.Complete();
});
備註
XAML 運行時間的關機會系結至線程上執行的 DispatcherQueue 關機順序。 如需詳細資訊,請參閱 DispatcherQueue 檔。
當使用 XAML 的線程上的 DispatcherQueue 關機時,會依序引發這些事件:
- 會引發 DispatcherQueue.ShutdownStarting 事件。 適用於要處理的應用程式。
-
會引發 DispatcherQueue.FrameworkShutdownStarting 事件。 適用於要處理的架構。
- WindowsXamlManager.XamlShutdownCompletedOnThread 事件會引發,以回應 FrameworkShutdownStarting。 適用於要處理的應用程式。
- 會引發 DispacherQueue.FrameworkShutdownCompleted 事件。 此時,DispatcherQueue 不適用於新的工作。
- 會引發 DispacherQueue.ShutdownCompleted 事件。 此時,DispatcherQueue 不適用於新的工作。
在 引發 WindowsXamlManager.XamlShutdownCompletedOnThread 事件時:
- XAML 已卸除所有即時 XAML 物件,並針對每個物件引發 Unloaded 事件。
- XAML 不再有任何與目前線程相關聯的狀態。
WindowsXamlManager.GetForCurrentThread 此時會傳
null
回。 - 目前線程上的 DispatcherQueue 仍可供使用。 其處於關機順序,因此請注意 ShutdownStarting 事件已經引發,而且不會再次引發。
注意
即使您在 WindowsXamlManager 物件上呼叫 Close,或釋放它的所有參考,仍會引發此事件。