Simplest possible event communication needs.

Topics: Prism v4 - WPF 4
May 28, 2015 at 12:41 PM
Hey all!

I am working on a WPF app that has a self hosted WCF service in it. This all works just fine. What I would like to do is trigger some interactivity code that lives in a function on my MainWindow class when I get a specific WCF service call.

Now, all the WCF stuff is done. I only mention it because the WCF service is not (I believe) running in the UI thread and so I will need to take into account passing events across the UI thread boundary.

My problem is that all of the relevant code samples I can find fall into 2 categories...
  1. For previous versions of Prism than 5.0 and not very detailed at that
  2. Absolutely dependent on a whole mess of other Prism 5 goodness (modules, MVVM and so on) that I am not in a position to graft into my project right now.
What I would love, if anyone had it was a pointer to a sample (or some code snippets) that showed the following...
  • How to declare and initialize the Microsoft.Practices.Prism.PubSubEvents.EventAggregator object when I am not using an dependency injection tool. I assume I need to initialize it in the App.xaml.cs class on on of the App events... but there are no samples I can find.
  • Once it is initialized, how to I reference it from MainWindow.cs so that I am getting the correct instance?
  • Simple pub / sub / unsub code would be nice - but if I can get the two points above squared away I can figure that out from the examples.
Anyway, thanks for any help you might offer!

Ken
May 29, 2015 at 1:08 PM
I'm new to this but hope I can help anyway. I have the following example:

Create a PubSubEvent class: public class UserUpdatedEvent : PubSubEvent<Employee> { }

Employee is the type which should be published when changes occur:

Publish the event where CurrUser (of type Employee) is updated: eventAggregator.GetEvent<UserUpdatedEvent>().Publish(CurrUser);

CurrUser is the property of type Employee: Public Employee CurrUser { get { return _currUser; } set { SetProperty(ref _currUser, value); } }

Subscribe to the event where you want to update the values: eventAggregator.GetEvent<UserUpdatedEvent>().Subscribe(UserUpdated);

UserUpdated is the method which support the updated CurrUser as parameter: private void UserUpdated(UserDbDomains.Employee currUser) {CurrUser = currUser;}

Where appropriate, use the: using Microsoft.Practices.Prism.PubSubEvents;

I get the eventAggregator from the parameter of the constructor: public CompanyViewViewModel(EventAggregator eventAggregator) { }

My case is part of a more complex Prism-application, but I believe the EventAggregator will work as a "stand-alone" as well. It would anyway be interesting to know if it does in order to better understand the requirements for using the EventAggregator in other Applications.

Rgds
Hans Kr.