Creating a simple WPF application using Prism: Part 3


In Part 2, we discussed on how to go about creating Modules in Prism, so how do we go about getting one module to talk to another? For this portion, I’m going to explain how to get the Search Module to talk to the Navigation Module. (btw, the source code for this app can be downloaded at the end of this post)

EventAggregator

First, let create a new Class Library project, called Infrastructure. As the name implies, this is the library that will hold all your common classes and will be referenced by all your other modules.

VS_InfraIf you’ve been wondering where the RegionNames static class I’ve been using is, your answer is on the left. The important thing to take note of is the Event classes in the Events Folder. These are the events that we will use to propagate to the modules.

By the way, I created a CommandFactory to create custom commands, but turns out that Prism has a DelegateCommand<T> class that does pretty much the same thing and more.

Point to note is that all your event classes will be stored in this Infrastructure library, and all your modules will need to reference this library.

To create an event that the Event Aggregator can understand, your events will need to inherit from CompositePresentationEvent<T>. Here’s the code for SearchEvent.cs.

using Microsoft.Practices.Composite.Presentation.Events;

namespace WpfComposite.Infrastructure
{
    public class SearchEvent : CompositePresentationEvent<string>
    {
    }
}

Publishing Events
In order to publish this SearchEvent, you will typically perform that in your ViewModel. The snippet of SearchViewModel code in Part 2 illustrates that, where we grab a reference of IEventAggregator in the ViewModel‘s constructor. To publish this event, you do it like so…

_eventAggregator.GetEvent<searchevent>().Publish("SomeText");

Subscribing Events
To subscribe to that event in other Modules, you only need to again get a reference to IEventAggregator in your ViewModel‘s constructor, and subscribe to that event…like so.

private Microsoft.Practices.Composite.Events.IEventAggregator _eventAggregator;

public SomeOtherViewModel(IEventAggregator eventAggregator)
{
    eventAggregator.GetEvent</searchevent><searchevent>().Subscribe(SearchTriggered);
}

private void SearchTriggered(string searchText)
{
    // do something here
}

That’s all folks

Personally, I totally enjoyed the processing of learning about Prism, the end result is just a bonus. I hope when you start writing your own Prism applications, you can stop from time to time and adopt a top-down view to see how everything fits together, and appreciate the benefits of having a decoupled, testable and flexible application.

I hope these series of post has been helpful and til next time, happy coding.

Download Prism v2 Feb 2009 release.
Download sample code for this application.

Share this post:

Advertisements
Posted in WPF. Tags: . 1 Comment »

One Response to “Creating a simple WPF application using Prism: Part 3”

  1. NDepend: A product you can depend on. « Code Blitz Says:

    […] a sample project I’ve been working on slowly and painfully over the last month. I took the sample WPF application (using Prism v2) I posted some time back, and decided to convert it into a Multi-Target project with Silverlight using Project Linker (will […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: