Pass two or more parameters via EventAggregator

Topics: Prism v4 - WPF 4
Feb 28, 2013 at 6:28 PM
Sometimes i need to pass two or more parameters between modules when using EventAggregator.
Is it possible? If not possible what is the alternative?
Tanks in advance.
Feb 28, 2013 at 8:46 PM

As far as I know, when publishing an event you can only pass one parameter of the same type as the event's payload. For example, if you define a MyEvent doing something like this:
public class MyEvent : CompositePresentationEvent< MyParameterClass >
It means you can pass an object of type MyParameterClass when publishing the event.

However, this gives you a lot of flexibility as the MyParameterClass could be as simple or as complex as you want. Taking this into account, a possible approach to pass several parameters could be encapsulate those parameters in a single one used to contain them:
parameters.Parameter1 = parameter1;
parameters.Parameter2 = parameter2;

Damian Cherubini
Mar 1, 2013 at 5:04 AM
Hi Damian,
Thanks Damian i applied this approach and it is working ok. but is it better to use class or struct as payload? or no difference?
Mar 1, 2013 at 5:53 PM

I'm not aware of any "rule" that says you cannot use a struct as the payload type for a CompositionEvent , but the usual approach is to use a class . Although it's true that in some cases using a struct can report a better performance than using a class , I believe this can only be observed when managing large collections of data, so it doesn't seem to me that a struct would provide you any benefit over a class in this scenario.

In my opinion, I would use a class as to keep the concordance with the rest of events you might have in your application.


Damian Cherubini