在 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);