2

在 C++/CX 中订阅事件是这样的:

listener::ConnectionReceived +=
ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>(this, &MyClass::OnConnectionReceived);

我找到的关于如何在 WRL 中订阅事件的所有文档都显示了使用 lambda 表达式的示例,如下所示:

auto connectionReceivedHandler = Callback<ITypedEventHandler<StreamSocketListener*, StreamSocketListenerConnectionReceivedEventArgs*>>
([&] (IStreamSocketListener* cbListener, IStreamSocketListenerConnectionReceivedEventArgs* args)
{
    this->doSomething(); 
});
hr = listener->add_ConnectionReceived(connectionReceivedHandler.Get(), &this->connectionReceivedToken);

但是如何订阅 WRL 中的事件并提供类方法而不是 lambda?像这样的东西:

hr = listener->add_ConnectionReceived(&MyClass::OnConnectionReceived, &this->connectionReceivedToken);
4

2 回答 2

0

我不熟悉 WRL,但由于它支持 C++11 lambda,我相信它也应该支持std::bind

auto callback = Callback<ITypedEventHandler<StreamSocketListener*,
                                       StreamSocketListenerConnectionReceivedEventArgs*>>
  (std::bind(
    &MyClass::OnConnectionReceived,
    ptr_to_instance_of_MyClass,  // eg. this
    std::placeholders::_1,       // cbListener
    std::placeholders::_2        // args
  ));

hr = listener->add_ConnectionReceived(callback.Get(), &this->connectionReceivedToken);
于 2013-10-03T00:39:24.583 回答
0

有一个回调的重载,它采用这个 + 成员函数。这里是...

ComPtr<typename Details::DelegateArgTraitsHelper<TDelegateInterface>::Interface> Callback(_In_ TCallbackObject *object, _In_ HRESULT(TCallbackObject::* method)(TArgs...)) throw()
于 2017-07-07T16:28:53.827 回答