winrt::static_lifetime Markerstruktur (C++/WinRT)

Ein Markertyp, der an die übergeben wird, implementiert die Basisstruktur einer Aktivierungsfactory , um sie für die statische Lebensdauer zu aktivieren (um sie anzuheften ). Ein Verwendungsbeispiel für Markertypen finden Sie unter Markertypen.

Syntax

struct winrt::static_lifetime

Beispiele

Hier ist ein bestimmtes Beispiel für winrt::static_lifetime. Wenn die Aktivierungsfactory für MyRuntimeClass ein Singleton sein soll, heften Sie sie wie folgt an.

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

Sie sollten statische Ereignisse für Ihr Aktivierungsfactoryobjekt mithilfe des Markers winrt::static_lifetime implementieren. Sie heften die Factory an, um sicherzustellen, dass ihre Lebensdauer stabil ist, wenn Ereignishandler hinzugefügt und entfernt werden. Sie können all dies in einem Header wie in diesem nächsten Beispiel durchführen.

Hinweis

Ein neues C++/WinRT-Projekt (aus einer Projektvorlage) verwendet standardmäßig Komponentenoptimierungen . Wenn Sie keine Komponentenoptimierungen verwenden, können Sie den angegebenen Teil der Auflistung weglassen.

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

Anforderungen

Mindestens unterstütztes SDK: Windows SDK Version 10.0.17763.0 (Windows 10, Version 1809)

Namespace: winrt

Header: %WindowsSdkDir%IncludeWindowsTargetPlatformVersion<>\cppwinrt\winrt\base.h (standardmäßig enthalten)

Siehe auch