Compartilhar via


WindowsXamlManager.XamlShutdownCompletedOnThread Evento

Definição

Ocorre quando o runtime XAML termina seu processo de desligamento no thread atual.

// 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) 

Tipo de evento

Exemplos

Este exemplo demonstra como você pode assinar o evento para limpo seus objetos quando o XAML não estiver mais em execução no thread.

Neste exemplo, você tem alguns tipos diferentes de objetos que deseja limpo à medida que o aplicativo é desligado. O XAML tem algumas referências indiretas a alguns desses objetos (por exemplo, objetos "Model", como em um aplicativo Model-View-ViewModel), portanto, você não deseja destruí-los até que o XAML seja concluído com seu trabalho. Você também tem alguns objetos que estão sendo limpos em outro processo e deseja aguardar por eles também.

Portanto, você usa o evento XamlShutdownCompletedOnThread e seu adiamento para organizar o processo de desligamento. Quando esse evento é gerado, você sabe que o XAML é feito usando seus objetos neste thread. Você também pode fazer um adiamento para que o desligamento não seja concluído até que suas operações remotas sejam concluídas.

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();
    });

Comentários

O desligamento do runtime XAML está vinculado à sequência de desligamento do DispatcherQueue em execução no thread. Para obter mais informações, consulte a documentação do DispatcherQueue.

Quando um DispatcherQueue em um thread que usa XAML é desligado, esses eventos são gerados na ordem:

No momento em que o evento WindowsXamlManager.XamlShutdownCompletedOnThread é gerado:

  • O XAML descarregou todos os objetos XAML dinâmicos e gerou o evento Unloaded para cada objeto.
  • O XAML não tem mais nenhum estado associado ao thread atual. WindowsXamlManager.GetForCurrentThread retorna null neste momento.
  • O DispatcherQueue no thread atual ainda está disponível e utilizável. Ele está em sua sequência de desligamento, portanto, observe que o evento ShutdownStarting já foi acionado e não será gerado novamente.

Observação

Mesmo que você chame Close no objeto WindowsXamlManager ou libere todas as suas referências a ele, esse evento ainda será gerado.

Aplica-se a