struct de marcador winrt::static_lifetime (C++/WinRT)

Um tipo de marcador, que é passado para o implementa o struct base de uma fábrica de ativação para adote-o ao tempo de vida estático (para fixá-lo ). Para ver um exemplo de uso de tipos de marcador, consulte Tipos de marcador.

Sintaxe

struct winrt::static_lifetime

Exemplos

Aqui está um exemplo específico de winrt::static_lifetime. Se você quiser que o fábrica de ativação para MyRuntimeClass seja um singleton, fixe-o desta forma.

// MyRuntimeclass.h
#pragma once

#include "MyRuntimeClass.g.h"

namespace winrt::MYNAMESPACE::implementation
{
    struct MyRuntimeClass : MyRuntimeClassT<MyRuntimeClass> { ... };
}

namespace winrt::MYNAMESPACE::factory_implementation
{
    struct MyRuntimeClass : MyRuntimeClassT<MyRuntimeClass, implementation::MyRuntimeClass, static_lifetime>
    {
    };
}

Você deve implementar eventos estáticos em seu objeto de fábrica de ativação usando o marcador winrt::static_lifetime . Fixe a fábrica para garantir que seu tempo de vida seja estável à medida que os manipuladores de eventos são adicionados e removidos. Você pode fazer tudo isso em um header, como neste próximo exemplo.

Observação

Um novo projeto C++/WinRT (de um modelo de projeto) usará otimizações de componente por padrão. Se você não estiver usando otimizações de componente, poderá omitir a parte da listagem indicada.

// MyRuntimeClass.h
#pragma once

#include "MyRuntimeClass.g.h"

// Forward-declare the instance class.
namespace winrt::Component::implementation
{
    struct MyRuntimeClass;
}

// Then define the activation factory class before the instance class.
namespace winrt::Component::factory_implementation
{
    struct MyRuntimeClass : MyRuntimeClassT<MyRuntimeClass, implementation::MyRuntimeClass, static_lifetime>
    {
        winrt::event_token MyRuntimeClassEvent(Windows::Foundation::EventHandler<int32_t> const& handler)
        {
            return m_static.add(handler);
        }

        void MyRuntimeClassEvent(winrt::event_token const& cookie)
        {
            m_static.remove(cookie);
        }

        void RaiseMyRuntimeClassStaticEvent(int32_t value)
        {
            m_static(nullptr, value);
        }

        event<Windows::Foundation::EventHandler<int32_t>> m_static;
    };
}

namespace winrt::Component::implementation
{
    struct MyRuntimeClass
    {
        MyRuntimeClass() = delete;

        // If you're not using component optimizations, then you can omit these next three methods.

        // Component optimizations means that you have to implement any statics on the instance class,
        // and have those forward to the activation factory. You will see build errors if you don't do this.

        static winrt::event_token MyRuntimeClassStaticEvent(Windows::Foundation::EventHandler<int32_t> const& handler)
        {
            return make_self<factory_implementation::MyRuntimeClass>()->MyRuntimeClassStaticEvent(handler);
        }

        static void MyRuntimeClassStaticEvent(winrt::event_token const& cookie)
        {
            return make_self<factory_implementation::MyRuntimeClass>()->MyRuntimeClassStaticEvent(cookie);
        }

        static void RaiseMyRuntimeClassStaticEvent(int32_t value)
        {
            return make_self<factory_implementation::MyRuntimeClass>()->RaiseMyRuntimeClassStaticEvent(value);
        }
    };
}

Requisitos

SDK mínimo com suporte: Windows SDK versão 10.0.17763.0 (Windows 10, versão 1809)

Namespace: winrt

Header: %WindowsSdkDir%IncludeWindowsTargetPlatformVersion<>\cppwinrt\winrt\base.h (incluído por padrão)

Confira também