简体   繁体   English

使用MessagingCenter和标准.NET事件处理程序为感兴趣的各方通知更改有什么区别?

[英]What is the difference between using MessagingCenter and standard .NET event handlers for informing interested parties of changes?

What is the difference between using MessagingCenter and standard .NET event handlers for informing interested parties of changes? 使用MessagingCenter和标准.NET事件处理程序为感兴趣的各方通知更改有什么区别?

Two (untested) implementations of the same thing are below to demonstrate: 下面是两个(未经测试的)同一事物的实现来演示:

public class FooClass {
  public event EventHandler SomeEvent;

  public void DoSomeWork() {
     // ... stuff
    if(SomeEvent != null)
      SomeEvent(this, EventArgs.Empty);
  }
}

public class BarClass {
  FooClass _foo;

  public BarClass() {
    _foo = new FooClass();
    _foo.SomeEvent += delegate {
      // ... did something
   };
  }
}

Verses: 歌诀:

public class FooClass {
  public const string SomeEventName = "SomeEvent";
  public void DoSomeWork() {
    // ... stuff
    MessagingCenter.Send<FooClass>(this, SomeEventName);
  }
}

public class BarClass : IDisposable {
  public BarClass() {
    MessagingCenter.Subscribe<FooClass>(this, FooClass.SomeEventName, delegate {
      // .. did something
   });
  }

  public void Dispose() {
    MessagingCenter.Unsubscribe<FooClass>(this, FooClass.SomeEventName);
  }
}

From what I can tell there doesn't seem to be any difference, but if anyone can suggest any pros or cons for either, that'd help me understand. 从我可以告诉那里似乎没有什么区别,但如果有人可以建议无论任何优点缺点 ,那会帮助我理解。 Currently, I've been using event handlers. 目前,我一直在使用事件处理程序。

Is there any point in switching to using MessagingCenter? 切换到使用MessagingCenter有什么意义吗? Or any new best practice? 还是任何新的最佳做法?

The MessagingCenter from Xamarin is used to reduce coupling between ViewModels, as the sender and receiver do not need to know each other. Xamarin的MessagingCenter用于减少ViewModel之间的耦合,因为发送方和接收方不需要彼此了解。

You can still build a similar structure by creating something like an "EventHub"/"EventAggregator" which knows sender and receiver and uses .NET events. 您仍然可以通过创建类似“EventHub”/“EventAggregator”的东西来构建类似的结构,它知道发送者和接收者并使用.NET事件。

The MessagingCenter itself is kind of an EventAggregator MessagingCenter本身就是一种EventAggregator

EventAggregator的图片

ImageSource : https://msdn.microsoft.com/en-us/library/ff921122.aspx ImageSource: https//msdn.microsoft.com/en-us/library/ff921122.aspx

Here is a nice explanation of EventAggregators. 以下是EventAggregators的一个很好的解释。

An Event Aggregator is a simple element of indirection. Event Aggregator是一个简单的间接元素。 In its simplest form you have it register with all the source objects you are interested in, and have all target objects register with the Event Aggregator. 在最简单的形式中,您可以注册所有您感兴趣的源对象,并使所有目标对象都注册到Event Aggregator。 The Event Aggregator responds to any event from a source object by propagating that event to the target objects. Event Aggregator通过将该事件传播到目标对象来响应源对象中的任何事件。

To Answer the question: 回答这个问题:

Is there any point in switching to using MessagingCenter? 切换到使用MessagingCenter有什么意义吗? Or any new best practice? 还是任何新的最佳做法?

If you are not using something like an EventAggregator it is a good choice to switch to the MessagingCenter, or build an EventAggregator on your own. 如果您没有使用类似EventAggregator的东西,那么切换到MessagingCenter 自行构建EventAggregator是一个不错的选择。 As Saruman made a good hint on explaining what coupling is. 萨鲁曼在解释什么是耦合方面做了很好的暗示。 You allways want to reduce coupling for a clean code. 你总是希望减少耦合以获得干净的代码。

If you have access to those classes (ie from where you want to call your methods) then there really is not a lot of difference. 如果您可以访问这些类(即从您想要调用方法的位置),那么确实没有太大区别。

However if you don't have access to those classes(ie inside a view model or decoupled class) then message subscription event aggregation is a useful tool 但是,如果您无权访问这些类(即在视图模型或解耦类中),那么消息订阅事件聚合是一个有用的工具

Message center reduces coupling and enables view models and other components to communicate with without having to know anything about each other besides a simple Message contract. 消息中心减少了耦合,使视图模型和其他组件能够进行通信,除了简单的消息合同之外,无需了解彼此的任何信息。

Coupling 耦合

In software engineering, coupling is the degree of interdependence between software modules; 在软件工程中,耦合是软件模块之间相互依赖的程度; a measure of how closely connected two routines or modules are the strength of the relationships between modules. 衡量两个例程或模块之间紧密联系的程度是模块之间关系的强弱程度。

  • MessagingCenter is basically used in the Model-View-ViewModel pattern. MessagingCenter基本上用于Model-View-ViewModel模式。
  • It is a great way to communicate and pass data or notify about updates among ViewModels without knowing who sent it to whom via a simple Message Contract. 这是一种很好的方式来通信和传递数据或在ViewModels之间通知更新,而无需知道是谁通过简单的消息合同将其发送给谁。
  • Ex. 防爆。 In one screen if you make any service call to fetch the new data and you want to notify other screens to update their UI via broadcasting message from your current screen, then MessagingCenter is the best approach. 在一个屏幕中,如果您进行任何服务调用以获取新数据,并且您希望通过当前屏幕上的广播消息通知其他屏幕更新其UI,则MessagingCenter是最佳方法。
  • It decouples them without making any dependency among ViewModels , while EventHandlers makes dependency and may prohibit something from being released. 它解耦它们而不会在ViewModels之间产生任何依赖关系,而EventHandlers会产生依赖关系并可能禁止发布某些内容。 You explicitly have to decouple event handlers from the events to better release the resources. 您必须明确地将事件处理程序与事件分离,以便更好地释放资源。
  • MessagingCenter should be applied when the receiver doesn't care who sent the message and the sender doesn't care who will receive it. 当接收者不关心谁发送消息而发送者不关心谁将接收消息时,应该应用MessagingCenter Events should be used when the receiver needs to know who sent the message, but the sender still doesn't care who handles it. 当接收者需要知道谁发送了消息时,应该使用事件,但发送者仍然不关心谁处理它。
  • It is good to use MessagingCenter over Events but, if you make too much use of too many Messages using MessagingCenter , it would be hard to identify who sent it and when sent it, the relation between messages would be hard to guess, thus making it hard time while debugging the app. 最好使用MessagingCenter不是Events但如果你使用MessagingCenter过多地使用过多的MessagingCenter ,就很难确定是谁发送了它,当发送它时,消息之间的关系很难被猜测,从而使它成为可能。调试应用程序时遇到困难。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中附加\\分离事件处理程序的不同方法有什么区别? - What is the difference between different ways of attaching\detaching event handlers in C# Silverlight - C# 和 VB.net 事件处理程序之间是否存在性能差异? - Silverlight - Is there a performance difference between C# and VB.net Event Handlers? 将两种不同类型的事件分配给事件处理程序的区别 - Difference between two different types of assigning events to event handlers 有代理处理程序的事件和没有代理处理程序的事件有什么区别? - What is the difference between Events with Delegate Handlers and those without? DatatableRowchanging Event和DataTableRowChanged事件有什么区别 - What is the difference between DatatableRowchanging Event and DataTableRowChanged Event 在视图模型之间使用时,MessagingCenter退订在xamarin表单中不起作用 - MessagingCenter Unsubscribe not working in xamarin forms when using between viewmodels 使用 do.net 和 MSBuild 构建 .NET 应用程序有什么区别? - What's the difference between using dotnet and MSBuild for building .NET applications? 将.NET任务与方法和动作一起使用有什么区别? - What the difference between using .NET tasks with method and action? 使用Observer / Event处理程序在两个类之间传递数据 - Pass data between 2 classes using Observer/Event handlers 使用自定义事件处理程序在表单之间传递数据 - Passing data between forms using custom event handlers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM