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 應用程式中的「Model」物件),所以你不想在 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 物件,並為每個物件設定了 卸載事件 。
- XAML 不再與目前執行緒有任何狀態相關聯。 此時 WindowsXamlManager.GetForCurrentThread 會回傳
null。 - 目前執行緒的 DispatcherQueue 仍然可用且可用。 它正在關機序列中,請注意 關機起始 事件已經被觸發,且不會再被觸發。
Note
即使你在 WindowsXamlManager 物件上呼叫 Close,或是解除所有對它的引用,這個事件還是會被觸發。