Sunday, September 12, 2010

How To : Communicate between two user-controls using Event Aggregator

I found many posts in the asp.net, silverlight.net fourms regarding communication between user-controls, so i thought to blog a bit about communication between user-controls using Event Aggregator in silverlight which i found is the best way to communicate.
The main advantage for using an event aggregator to communicate between user-controls is, it removes the tight coupling between a publisher and a subscriber, by which a publisher/subscriber can evolve independently. Pre-requisites for understanding this blog post is to have a bit understanding of Event Aggregator.
Live Demo : Event Aggregator Sample
Source Code: At the end of this post :).
In this post i will explain how two user-controls can pass messages between them using event aggregator, consider a simple chat scenario where one user types-in some message and sends to to another user and vice-versa. My Visual Studio Solution looks as shown below, things to observe are

  • Microsoft Practices Composite dll's which are required for implementing the event aggregation
  • EventAggregator.cs which implements the interface IEventAggregator. (Note: will share the enitre source code at the end of this blog post :) )
  • "Kranthi.xaml" and "Kiran.xaml", two user controls which communicate using event aggregator.

Read more: Dot + Net <=> Digging .Net