如何将事件处理程序分配给 C++/CLI 中的事件?
如何将“事件”添加到“事件”/委托?语法是什么? C++/CLI 和 C# 中的情况相同吗?
How do I add "events" to an "event"/delegate? What is the syntax?
Is it the same in C++/CLI and in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1:如果事件的底层代理是您自己定义的自定义代理,它是类成员(示例来自 MSDN):
2: 如果底层委托是全局处理程序并且具有 .NET 事件的标准签名(对象 + 事件参数)(来自 DPD 答案):
3: 如果底层委托具有 .NET 事件的标准签名,并且事件处理程序是一个类方法:
4:使用 System::EventHandler 泛型(采用 MyEventArgs args 参数)作为底层委托:
1: If underlyng delgate of the event is a custom one you define yourself that is a class memeber (example from MSDN):
2: If the underlying delegate is a global handler and has the standard signature for .NET events (object + event args) (from DPD answer):
3: If the underlying delegate has the standard signature for .NET events and the event handler is a class method:
4: Using System::EventHandler generic (that takes a MyEventArgs args parameter) as the underlying delegate:
在 c# 中,您可以使用 < code>+= 运算符:
...
超过一年后的编辑
自从我发布这个答案以来已经很长时间了,有人注意到我可能是错误的。我真的不知道为什么OP将我的答案标记为正确的答案(也许OP正在寻找这个而不是c++-cli 语法?现在谁知道)。
无论如何,在 c++ 中-cli 它将是:
In c#, you do it with the
+=
operator:...
More-than-a-year-later-edit
It has been a long time since I posted this answer and someone noticed me that maybe it was wrong. I really don't know why the OP marked my answer as the right one (maybe OP was looking for this rather than c++-cli syntax? Who knows now).
Anyway, in c++-cli it would be:
C++/CLI 的语法是:
为事件注册:
The syntax for C++/CLI is :
to register this for an event: