如何订阅另一个程序集中引发的事件

发布于 2024-08-18 10:58:29 字数 135 浏览 6 评论 0原文

我有一个包含 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 技术交流群。

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

发布评论

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

评论(3

毁梦 2024-08-25 10:58:29

在单独的程序集中引发的事件没有什么特别之处,除非事件本身是使用 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 - but Button is in a different assembly, isn't it?

空心空情空意 2024-08-25 10:58:29

该事件必须在定义它的类中声明为公共:

public class Something
{
  public event EventHandler SomethingCompleted;
}

然后您可以像订阅任何其他事件一样订阅它:

Something s = ...;
s.SomethingCompleted += SomeEventHandler;

The event must be declared as public in the class which defines it:

public class Something
{
  public event EventHandler SomethingCompleted;
}

You can then subscribe to it just as to any other event:

Something s = ...;
s.SomethingCompleted += SomeEventHandler;
独守阴晴ぅ圆缺 2024-08-25 10:58:29

您需要引用引发事件的类来注册事件处理程序。

完成后,就像注册任何其他事件处理程序一样完成:

foo.SomethingCompleted += (sender, e) => this.DoSomething();

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:

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