Dynamically load module AND pass it data....HOW?

Topics: Prism v2 - WPF 3.5
Mar 24, 2010 at 4:05 PM

I have a viewmodel which has a DelegateCommand for handling a button.  When the button is clicked, I need to load a view from a different module and pass it some data.  I can load the view via a module controller which will dynamically load a module into a region.  I can pass the new view's viewmodel data via an event.  The problem is that the module (and thus the view/viewmodel) is not yet loaded when the event to pass it data is published.  So, what is the right way to do this?

Here are some pseudo code snippets:

ViewModel 1:
private void ShowSomething(object obj)
   //load the module in the given view
   _eventAggregator.GetEvent<ModuleChangeEvent>().Publish(new ModuleChangeInfo() { ModuleName = ModuleNames.Module1, Region = RegionNames.MainRegion });


ViewModel 2:
public ViewModel2(IEventAggregator eventAggregator)
   this._eventAggregator = eventAggregator;


Module Controller:
IRegion region = _regionManager.Regions[regionName];
object currentView = region.GetView(regionName);

if (currentView != null)

currentView = module.View;
region.Add(currentView, regionName);

Perhaps this is entirely the wrong the approach.  Any help is greatly appreciated.

Mar 24, 2010 at 7:45 PM

So I have now seen how I can do this via a Controller.  However, this seems to force me to have a 'View' interface property in my ViewModel and a 'Model' property in my View.  I don't particularly like this fact as I'd rather that the ViewModel knows nothing about the view itself. 

At this point, I'm just wondering if the scenario above is still OK and if someone has any other thoughts.

Tks again.

Mar 25, 2010 at 1:31 AM


I noticed that in your module controller you are removing a view from a region and adding a new one. If your application’s scenario requires only one of the views to be active in that region at the same time, you could use a SingleActiveRegion (use a control that inherits from ContentControl), and simply activate/deactivate the views you require. Matias Bonaventura explains some of the different view states in this post.

Related to the module not having been instantiated when the event is published issue, you could check out Ade Miller’s post about Adding Store and Forward support to the Prism EventAggregator for some ideas that might help with that.

Finally, Erwin Van Der Valk, developer in the Prism-v2 project, has created an Outlook style sample application that also shows a different UI based on user input (sort of from a sidebar) which you might find useful.

Please let me know if this helps.

Damian Schenkelman

Mar 25, 2010 at 10:25 PM

Thanks for the links!  Very good stuff.