winrt::static_lifetime 표식 구조체(C++/WinRT)

정적 수명(고정)에 옵트인하기 위해 활성화 팩터리의 기본 구조체 구현에 전달되는 표식 형식입니다. 표식 형식의 사용 예제는 표식 형식을 참조하세요.

구문

struct winrt::static_lifetime

다음은 winrt::static_lifetime 특정 예제입니다. MyRuntimeClass에 대한 정품 인증 팩터리를 싱글톤으로 지정하려면 다음과 같이 고정합니다.

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

winrt::static_lifetime 표식을 사용하여 정적 이벤트를 활성화 팩터리 개체에 구현해야 합니다. 이벤트 처리기가 추가되고 제거될 때 수명이 안정적으로 유지되도록 팩터리를 고정합니다. 다음 예제와 같이 헤더에서 이 모든 작업을 수행할 수 있습니다.

참고

프로젝트 템플릿의 새 C++/WinRT 프로젝트는 기본적으로 구성 요소 최적화를 사용합니다. 구성 요소 최적화를 사용하지 않는 경우 표시된 목록의 일부를 생략할 수 있습니다.

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

요구 사항

지원되는 최소 SDK: Windows SDK 버전 10.0.17763.0(Windows 10, 버전 1809)

네임스페이스: winrt

헤더: %WindowsSdkDir%IncludeWindowsTargetPlatformVersion<>\cppwinrt\winrt\base.h(기본적으로 포함)

참고 항목