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 應用程式中的「模型」物件) ,因此在 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 事件時:

注意

即使您在 WindowsXamlManager 物件上呼叫 Close,或釋放它的所有參考,仍會引發此事件。

適用於