2
votes

I'm currently familiarizing myself with Prism 4.0 and MVVM (using Cinch 2.0 as the MVVM framework.) I'm building the shell for what will be an application with roughly 15 modules. We're using WPF.

The application uses a ribbon for its menu system. Modules are loaded when the user clicks on a button on the ribbon (the shell handles that task through its viewmodel.) The modules register their main view with the region manager (the shell defines only one main region, for now anyway.) Most modules are loaded on demand. This works fine in a simple scenario:

  • User clicks on a button. Module gets loaded, view is added to the region and displayed.
  • User clicks on a button for some other module. Same idea as above. The view for that module is displayed in the same region, replacing the view from the previous module.

Where things stop working is when the user then clicks again on the button for a previously loaded module. Since the module has already been loaded, pretty much nothing happens - the current view remains displayed on screen. That makes sense - LoadModule is pretty much ignored (or at least it looks like it is being ignored.)

I've done quite a few searches on Google for this, and most answers I found required the shell to navigate to the view. Now, unless I'm mistaken, this is coupling the shell to the modules a bit too much: The shell shouldn't know about the view(s) in any module. Essentially this seems to break the MVVM pattern. The shell knowing about the modules is a necessary coupling (in our case anyway - we don't (can't) use stuff like directory discovery. But the shell knowing about each module's views is pushing it, in my opinion.

Bottom line, the question is: How can I navigate between existing views, using a ribbon interface (well, the fact that it's a ribbon has no bearing on this) which is controlled by the shell, by having the shell "tell" an already-loaded module "hey, you've got focus again, so display whatever view was displayed the last time you were the star of the show."

Then again maybe I'm just going at this the wrong way... Wouldn't be a first. :)

Thanks!

3

3 Answers

1
votes

If you do not want to activate the view from your shell, then what you can do is to create a CompositePresentationEvent which will contain the name of the module or whatever the kind of data that could identify a precise module. Then broadcast this event in the whole application with the EventAggregator from Prism. Finally make your modules listen to this event. If they are identified in the event as the module required to activate their views, then just put the code to activate the views.

Have a look at the answer of this question: EventAggregator communication in Prism The sample covers how to communicate inside a Prism application without knowing the recipient.

0
votes

Maybe you could solve the problem bye with the button click you not only load the model then you iterate through the already loaded modules and if yours already exist you set is as active in his ViewModel and the View could look for a property in the ViewModel if it is active or not, and you notify the View with the INotifyPropertyChange.

It is only a Wild guess i'm new to the MVVM and even newer to Prims :D

0
votes

Under your RPISM installation take a look at

Prism\Quickstarts\View-Switching Navigation

Your Shell doesn't need to know about module. RegionManager will take care about loading Views on demand.

This example will show you how buttons (radiobuttons on left) use Navigation to change views. And you can control with MEF if same or new instance created, etc.

Idea is that you REGISTER view only for menu (your ribbon). Then you just Navigate. Let me know if you need more information