winrt::static_lifetime marker struct (C++/WinRT)

Tipo di marcatore, che viene passato alla struttura di base implements di una factory di attivazione per acconsentire esplicitamente alla durata statica (per bloccarla ). Per un esempio di utilizzo dei tipi di marcatori, vedere Tipi di marcatore.

Sintassi

struct winrt::static_lifetime

Esempio

Ecco un esempio specifico di winrt::static_lifetime. Se si vuole che la factory di attivazione per MyRuntimeClass sia un singleton, aggiungere la factory in questo modo.

// 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>
    {
    };
}

È necessario implementare eventi statici nell'oggetto factory di attivazione usando il marcatore winrt::static_lifetime . Aggiungere la factory per assicurarsi che la relativa durata sia stabile quando i gestori eventi vengono aggiunti e rimossi. È possibile eseguire tutte queste attività in un'intestazione, ad esempio in questo esempio successivo.

Nota

Un nuovo progetto C++/WinRT (da un modello di progetto) userà le ottimizzazioni dei componenti per impostazione predefinita. Se non si usano le ottimizzazioni dei componenti, è possibile omettere la parte dell'elenco indicata.

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

Requisiti

SDK minimo supportato: Windows SDK versione 10.0.17763.0 (Windows 10, versione 1809)

Spazio dei nomi: winrt

Intestazione: %WindowsSdkDir%IncludeWindowsTargetPlatformVersion<>\cppwinrt\winrt\base.h (incluso per impostazione predefinita)

Vedi anche