事件聚合...到底发生了什么?

发布于 2024-08-12 01:29:03 字数 704 浏览 6 评论 0原文

我经常想知道它,但现在我遇到了包含它的逻辑,我想我应该继续并获得一些破译基本原理的帮助。问题如下,我正在查看一个正在利用复合应用程序库的 WPF 应用程序。在应用程序的源代码中,我在视图的演示文稿中遇到了以下代码行。为了方便起见,我将其称为演示文稿 A:

private void OnSomethingChanged(SomeArgumentType arguement)
{
   UnityImplementation.EventAggregator.GetEvent<EventA>().Publish(null);
}

当我在上面给定的方法中看到方法 Publish 时,我的直觉告诉我在另一个类中的某处一定有一个订阅,我将其称为演示文稿 B,其中包含以下内容:

UnityImplementation.EventAggregator.GetEvent(Of EventA).Subscribe(AddressOf OnSomeEventA)

同一个类中有一个名为 OnSomeEventA 的私有函数,其中包含一些逻辑。

我的问题是这里的所有东西都是如何连接的?这里的“发布”“订阅”到底实现了什么?当“某些内容”发生变化时,编译器如何知道它必须遵循 OnSomethingChanged 中的逻辑,即“发布”由另一个已描述事件处理程序逻辑的类“订阅”的事件?了解这个过程的底层接线将会非常有用。

谢谢

I have often times wondered about it but now that I have encountered a piece of logic that incorporates it, I thought I should go ahead and get some help on deciphering the fundamentals. The problem is as follows, I am looking at a WPF application that is utilizing the Composite Application Library. Within the source of the application I came across the following line of code in the Presentation of a view. For the sake of convinience I will call it Presentation A:

private void OnSomethingChanged(SomeArgumentType arguement)
{
   UnityImplementation.EventAggregator.GetEvent<EventA>().Publish(null);
}

When I saw the method Publish in the above given method, my gut told me there must be a Subscribe somewhere and in another class, I will call it Presentation B there was the following:

UnityImplementation.EventAggregator.GetEvent(Of EventA).Subscribe(AddressOf OnSomeEventA)

There was a private function in the same class called OnSomeEventA that had some logic in it.

My question here is that how is everything wired over here? What exactly is achieved by the 'Publish' 'Subscribe' here? When 'something' changes, how does the compiler know it has to follow the logic in OnSomethingChanged that will 'Publish' an event that is 'Subscribed' by another class where the logic of the event handler has been described? It will be great to understand the underlying wiring of this process.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

趁年轻赶紧闹 2024-08-19 01:29:03

第一次为每个事件(由类型参数 T 标识)调用 GetEvent 时,EventAggregator 会创建一个空方法列表来当该事件发布时调用。通常,这会在第一次调用 PublishSubscribe 之前发生(如您的示例中所示)。

然后:

  • 每当Subscribe被调用时,一个方法就会被添加到列表中。
  • 每当调用 Publish 时,它都会遍历列表并进行这些调用。

因此,在演示文稿 A 中调用 Publish() 会导致调用通过调用 Subscribe 注册的所有方法,在您的示例中,这些方法将包括演示文稿 B 的 <代码>OnSomeEventA 方法。

尝试在 OnSomeEventA 方法中设置断点并查看堆栈,不要忘记源代码也可用!

The first time GetEvent<T> is called for each event (identified by the type parameter T) the EventAggregator creates an empty list of methods to call when that event is published. Typically, this will happen immediately before the first call to Publish or Subscribe (as in your examples).

Then:

  • Whenever Subscribe is called a method is added to the list.
  • Whenever Publish is called it walks through the list and makes those calls.

So, the call to Publish() in Presentation A results in all of the methods that have been registered by calling Subscribe being called, which in your example would include Presentation B's OnSomeEventA method.

Try setting a breakpoint in the OnSomeEventA method and take a look at the stack, and don't forget the source is available, too!

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