注册到 MediaStreamSample Processed 事件会出现 'H': is not a legal base class 错误

Registering to MediaStreamSample Processed event gives 'H': is not a legal base class error

我正在尝试订阅 MediaStreamSample Processed 事件,因此每当触发已处理事件时,都必须触发 OnSampleProcessed 方法 下面的实现是我尝试过的

StreamSamplePool.h

void OnSampleProcessed(winrt::Windows::Foundation::TypedEventHandler<MediaStreamSample, IInspectable const&> const& handler);

StreamSamplePool.cpp

void StreamSamplePool::OnSampleProcessed(winrt::Windows::Foundation::TypedEventHandler<MediaStreamSample, IInspectable const&> const& handler)
{
}

尝试使用以下代码订阅

sample.Processed(&StreamSamplePool::OnSampleProcessed);

添加以上代码行后得到 'H':不是合法基础 class 错误

调试输出-

1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\Generated Files\winrt\base.h(5231): error C2516: 'H': is not a legal base class
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\Generated Files\winrt\Windows.Foundation.h(889): message : see reference to class template instantiation 'winrt::impl::implements_delegate<winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>,H>' being compiled
1>        with
1>        [
1>            H=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\Generated Files\winrt\base.h(5280): message : see reference to class template instantiation 'winrt::impl::delegate<T,H>' being compiled
1>        with
1>        [
1>            T=winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>,
1>            H=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\Generated Files\winrt\Windows.Foundation.h(2504): message : see reference to function template instantiation 'T winrt::impl::make_delegate<winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>,_Ty>(H &&)' being compiled
1>        with
1>        [
1>            T=winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>,
1>            _Ty=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &),
1>            H=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\StreamSamplePool.cpp(57): message : see reference to function template instantiation 'winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>::TypedEventHandler<void(__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<D,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)>(L)' being compiled
1>        with
1>        [
1>            D=winrt::WinRTMediaLibrary::implementation::StreamSamplePool,
1>            L=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>D:\Prithvi\Work\Meeting\B2\Backup\UWP\WinRTMediaLibrary\StreamSamplePool.cpp(57): message : see reference to function template instantiation 'winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,winrt::Windows::Foundation::IInspectable>::TypedEventHandler<void(__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<D,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)>(L)' being compiled
1>        with
1>        [
1>            D=winrt::WinRTMediaLibrary::implementation::StreamSamplePool,
1>            L=void (__cdecl winrt::WinRTMediaLibrary::implementation::StreamSamplePool::* )(const winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Media::Core::MediaStreamSample,const winrt::implements<winrt::WinRTMediaLibrary::implementation::StreamSamplePool,winrt::WinRTMediaLibrary::StreamSamplePool>::IInspectable &> &)
1>        ]
1>C:\Program Files (x86)\Windows Kits\bin.0.19041.0\XamlCompiler\Microsoft.Windows.UI.Xaml.Common.targets(486,5): error MSB4181: The "CompileXaml" task returned false but did not log an error.
1>Done building project "WinRTMediaLibrary.vcxproj" -- FAILED

.

事件处理程序的签名有误。需要

void OnSampleProcessed(MediaStreamSample const& sender, IInspectable const& args);

或可以使用 MediaStreamSampleIInspectable 类型参数调用的类似签名。将成员函数订阅到事件也需要一个 class 实例指针。最简单的方法是让模板机制解决所有问题,然后通过

sample.Processed({ this, &StreamSamplePool::OnSampleProcessed });

Handle events by using delegates in C++/WinRT and Author events in C++/WinRT 详细解释基本概念。