Multiple instances of a view in separate modules.

Topics: Prism v2 - Silverlight 3
Aug 5, 2010 at 12:36 PM

Hi

I have a view in module A that contains a region in which a view in module B is displayed. The view in module B contains a treeview with a checkbox on each node. When a user clicks a checkbox I am publishing an event with the selected items as its payload and the view in module A subscribes to that event.

That’s all well and good but what if I add a view in module C that also contains a region in which the view in module B is displayed and also subscribes to the checkbox click event.

I require a separate instance of the view in module B in the two regions in module A and C.  How can I achieve this?

And how can I ensure that the subscribers to the event in Module B know which instance has published the event in order that they can filter for event they are interested in?

Is this scenario compatible with Prism? Is this use of events the best way to communicate between two modules when there are multiple instances of a view?

I hope that made sense. Any guidance would be appreciated.

Thanks

Kevin

Aug 5, 2010 at 8:36 PM
Edited Aug 5, 2010 at 9:14 PM

Hi Kevin,

I do not know you exact scenario, but if you need to create different instances of your View, you could take advance of the UI Composition in Prism by using the following approaches:

Both approaches could guarantee to you the creation of new instances of the view, as long as the singleton pattern wouldn't been used for its creation.

As for your second question, this might be achieved by applying filters when you call the Subscribe method.

In other words, you could notify your ViewB when it is displayed into RegionA or RegionC by implementing this on your own. And when you publish your event, you could use this information as an argument. So, when you call the Subscribe method, you could specify a predicate for filtering those event’s raises that shouldn’t be treated, as shown in the following code lines:

fundAddedEvent.Subscribe(FundAddedEventHandler, ThreadOption.UIThread, false, FundOrderFilter)
...
public bool
FundOrderFilter(FundOrder fundOrder)
{
    return fundOrder.CustomerId == _customerId;
}

For more information you could take a look at the EventAggregator Quickstart (see the ActivityPresenter class code line: 68).  

Please let me know if this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero