語言

WindowsXamlManager.XamlShutdownCompletedOnThread 事件

定義

當 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 被關閉時,會依序觸發以下事件:

WindowsXamlManager.XamlShutdownCompletedOnThread 事件被提出時:

Note

即使你在 WindowsXamlManager 物件上呼叫 Close,或是解除所有對它的引用,這個事件還是會被觸發。

適用於