事件聚合...到底发生了什么?
我经常想知道它,但现在我遇到了包含它的逻辑,我想我应该继续并获得一些破译基本原理的帮助。问题如下,我正在查看一个正在利用复合应用程序库的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一次为每个事件(由类型参数
T
标识)调用GetEvent
时,EventAggregator
会创建一个空方法列表来当该事件发布时调用。通常,这会在第一次调用Publish
或Subscribe
之前发生(如您的示例中所示)。然后:
Subscribe
被调用时,一个方法就会被添加到列表中。Publish
时,它都会遍历列表并进行这些调用。因此,在演示文稿 A 中调用
Publish()
会导致调用通过调用Subscribe
注册的所有方法,在您的示例中,这些方法将包括演示文稿 B 的 <代码>OnSomeEventA 方法。尝试在
OnSomeEventA
方法中设置断点并查看堆栈,不要忘记源代码也可用!The first time
GetEvent<T>
is called for each event (identified by the type parameterT
) theEventAggregator
creates an empty list of methods to call when that event is published. Typically, this will happen immediately before the first call toPublish
orSubscribe
(as in your examples).Then:
Subscribe
is called a method is added to the list.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 callingSubscribe
being called, which in your example would include Presentation B'sOnSomeEventA
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!