我应该何时在事件聚合器上使用事件处理程序?

时间:2022-01-19 20:47:35

When should I be using an Event Handler versus an Event Aggregator?

我何时应该使用事件处理程序而不是事件聚合器?

In my code, I have two ViewModels that controlled by a parent ViewModel, I am trying to decide if I should just use an event handler to talk between them? Or use an Event Aggregator? It is going to just be simple method call, I don't require parameters to be passed between them.

在我的代码中,我有两个由父ViewModel控制的ViewModel,我试图决定是否应该使用事件处理程序在它们之间进行通信?或者使用Event Aggregator?它只是简单的方法调用,我不需要在它们之间传递参数。

1 个解决方案

#1


5  

The way I see it, the EventAggregator is usually the heavy gun used when you want to publish an event to the entire application and more specifically - when you don't know who exactly is listening.

我看到它的方式,EventAggregator通常是你想要将事件发布到整个应用程序时使用的重枪 - 更具体地说 - 当你不知道究竟是谁正在收听时。

In your scenario that's not really the case, you have 2 view models that want to communicate, but they both know each other. So there is no real reason you can't use events.

在您的情况中并非如此,您有2个想要进行通信的视图模型,但它们彼此都知道。所以没有真正的理由你不能使用事件。

I'll just mention that if you want to keep it a little more loosely-coupled - make an interface for each of the viewmodels that exposes the event. This way each VM will use the other VM's Interface instead of a specific instance.

我只想提一下,如果你想保持它更松散耦合 - 为每个暴露事件的视图模型创建一个接口。这样,每个VM将使用另一个VM的接口而不是特定的实例。

#1


5  

The way I see it, the EventAggregator is usually the heavy gun used when you want to publish an event to the entire application and more specifically - when you don't know who exactly is listening.

我看到它的方式,EventAggregator通常是你想要将事件发布到整个应用程序时使用的重枪 - 更具体地说 - 当你不知道究竟是谁正在收听时。

In your scenario that's not really the case, you have 2 view models that want to communicate, but they both know each other. So there is no real reason you can't use events.

在您的情况中并非如此,您有2个想要进行通信的视图模型,但它们彼此都知道。所以没有真正的理由你不能使用事件。

I'll just mention that if you want to keep it a little more loosely-coupled - make an interface for each of the viewmodels that exposes the event. This way each VM will use the other VM's Interface instead of a specific instance.

我只想提一下,如果你想保持它更松散耦合 - 为每个暴露事件的视图模型创建一个接口。这样,每个VM将使用另一个VM的接口而不是特定的实例。