How to publish an EventAggregator event from the Shell?

Topics: Prism v2 - Silverlight 2, Prism v2 - WPF 3.5
Nov 30, 2009 at 5:07 AM

Hi,

Just a quick question: I have my shell with a button and 2 regions. I have created the necessary event classes and they work fine if I publish the event directly from the modules assigned to each region.

This is the constructor of one module

public itemsPresentationModel(Views.IListItemsView pv,IEventAggregator pea)
        {
            this.InitializeItems();
            this.ListView = pv;
            this.ListView.Model = this;
            this._eventAgregator = pea; // <----------- HERE I ASSIGN THE IEventAggregator to a local variable

            this.SelectCommand = new DelegateCommand<businessEntities.categoriesEntities>(OnSelect);
        } // itemsPresentationModel
And here I publish the event:
private void OnSelect(businessEntities.categoriesEntities pv)
        {
            PTTInfrastructure.SelectedItem lselectedItem =
                new PTTInfrastructure.SelectedItem { company = pv.company, store = pv.store, id = pv.category, text = pv.title1 };
            this._eventAgregator.GetEvent().Publish(lselectedItem);
        } // OnSelect
Not much complicated, but now I want from the shell to publish the event. I have a local IEventAggregator local variable, but how am I going to initialize it? 
I guess since this is going to be a simple call to publish the event I can use the Windows events in the Shell and from there publish the IEventAggregator event:
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            // What goes here?
        } // Shell()
I'll appreciate your comments.
Nov 30, 2009 at 1:38 PM

Hi,

One possibility could be having a presenter for the Shell and injecting the event aggregator when it is constructed. Then you will be able to make the calls as in any other place. If you modify the ShellPresenter class from the Stock Trader RI with the following code, you will be able to see an example of this:

private readonly IEventAggregator eventAggregator;

public ShellPresenter(IShellView view, IEventAggregator eventAggregator)
{
    View = view;
    this.eventAggregator = eventAggregator;
}

public IShellView View { get; private set; }

Another possibility could be using the Service Locator to get the instance of the Event Aggregator (you must reference the Microsoft.Practices.ServiceLocation assembly, located in the LIB folder):

this.eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Dec 1, 2009 at 3:58 AM

Damian,

Thank you very much. I opted for the second option:

using the Service Locator to get the instance of the Event Aggregator (you must reference the Microsoft.Practices.ServiceLocation assembly, located in the LIB folder)

And it worked great.

Thanks guys, it's always a pleasure to see we can get help from you anytime!

May 4, 2010 at 9:04 PM

Thanks Damien!