如何将事件处理程序分配给 C++/CLI 中的事件?

发布于 2024-10-16 20:17:42 字数 53 浏览 1 评论 0原文

如何将“事件”添加到“事件”/委托?语法是什么? 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咽泪装欢 2024-10-23 20:17:42

1:如果事件的底层代理是您自己定义的自定义代理,它是类成员(示例来自 MSDN):

delegate void Del(int, float);
ref class EventReceiver {
public:
    void Handler(int i , float f) {  }
};
myEventSource->MyEvent += gcnew Del(myEventReceiver, &EventReceiver::Handler);

2: 如果底层委托是全局处理程序并且具有 .NET 事件的标准签名(对象 + 事件参数)(来自 DPD 答案):

delegate void MyOwnEventHandler(Object^ sender, EventArgs^ e) { }  
myEventSource->MyEvent += gcnew EventHandler(MyOwnEventHandler);  

3: 如果底层委托具有 .NET 事件的标准签名,并且事件处理程序是一个类方法:

ref class EventReceiver {
public:
   void Handler(Object^ sender, EventArgs^ e) {  }
};
myEventSource->MyEvent += gcnew EventHandler(myEventReceiver, &EventReceiver::Handler);

4:使用 System::EventHandler 泛型(采用 MyEventArgs args 参数)作为底层委托:

ref class EventReceiver {
public:
   void Handler(Object^ sender, MyEventArgs^ e) {  }
};
myEventSource->MyEvent += gcnew EventHandler<MyEventArgs^>(this, &EventReceiver::DataReceived);

1: If underlyng delgate of the event is a custom one you define yourself that is a class memeber (example from MSDN):

delegate void Del(int, float);
ref class EventReceiver {
public:
    void Handler(int i , float f) {  }
};
myEventSource->MyEvent += gcnew Del(myEventReceiver, &EventReceiver::Handler);

2: If the underlying delegate is a global handler and has the standard signature for .NET events (object + event args) (from DPD answer):

delegate void MyOwnEventHandler(Object^ sender, EventArgs^ e) { }  
myEventSource->MyEvent += gcnew EventHandler(MyOwnEventHandler);  

3: If the underlying delegate has the standard signature for .NET events and the event handler is a class method:

ref class EventReceiver {
public:
   void Handler(Object^ sender, EventArgs^ e) {  }
};
myEventSource->MyEvent += gcnew EventHandler(myEventReceiver, &EventReceiver::Handler);

4: Using System::EventHandler generic (that takes a MyEventArgs args parameter) as the underlying delegate:

ref class EventReceiver {
public:
   void Handler(Object^ sender, MyEventArgs^ e) {  }
};
myEventSource->MyEvent += gcnew EventHandler<MyEventArgs^>(this, &EventReceiver::DataReceived);
辞旧 2024-10-23 20:17:42

中,您可以使用 < code>+= 运算符:

someObj.SomeEvent += new EventHandler(Blah_SomeEvent);

...

private void Blah_SomeEvent(object sender, EventArgs e)
{
}

超过一年后的编辑

自从我发布这个答案以来已经很长时间了,有人注意到我可能是错误的。我真的不知道为什么OP将我的答案标记为正确的答案(也许OP正在寻找这个而不是 语法?现在谁知道)

无论如何,在 它将是:

someObj->SomeEvent+= gcnew EventHandler(this, &Blah_SomeEvent);

In , you do it with the += operator:

someObj.SomeEvent += new EventHandler(Blah_SomeEvent);

...

private void Blah_SomeEvent(object sender, EventArgs e)
{
}

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 syntax? Who knows now).

Anyway, in it would be:

someObj->SomeEvent+= gcnew EventHandler(this, &Blah_SomeEvent);
遇到 2024-10-23 20:17:42

C++/CLI 的语法是:

delegate void MyOwnEventHandler(Object^ sender, Eventargs^ e)
{

}

为事件注册:

objectPtr->MyEvent += gcnew EventHandler(MyOwnEventHandler);

The syntax for C++/CLI is :

delegate void MyOwnEventHandler(Object^ sender, Eventargs^ e)
{

}

to register this for an event:

objectPtr->MyEvent += gcnew EventHandler(MyOwnEventHandler);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文