如何订阅另一个程序集中引发的事件
我有一个包含 3 个项目的解决方案。一个项目处理异步通信。当它完成回调时,它会引发一个事件 SomethingCompleted。 如何从同一解决方案中的另一个项目订阅此事件?
我在接收项目中内置了事件处理程序,但在发送项目中看不到该事件。
I have a solution which contains 3 project. One project handles asynchronous communinications. When it has completed it's callback, it raises an event SomethingCompleted.
How do I subscribe to this event from another project in the same solution?
I have the event handlers built in the receiving project but it does not see the event in the sending project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在单独的程序集中引发的事件没有什么特别之处,除非事件本身是使用
internal
访问修饰符声明的。检查它是否是公开的。举个例子来说明我的意思,我确信您不会再三考虑订阅
Button.Click
- 但Button
位于不同的程序集中,不是”是吗?There's nothing special about events raised in a separate assembly, unless the event itself is declared with the
internal
access modifier. Check that it's public.To give an example of what I mean, I'm sure you don't think twice about subscribing to
Button.Click
- butButton
is in a different assembly, isn't it?该事件必须在定义它的类中声明为公共:
然后您可以像订阅任何其他事件一样订阅它:
The event must be declared as public in the class which defines it:
You can then subscribe to it just as to any other event:
您需要引用引发事件的类来注册事件处理程序。
完成后,就像注册任何其他事件处理程序一样完成:
You need to have a reference to the class raising the event to register an event handler.
Once you have that, it's done like registering any other event handler: