How To Get View? (Prism v4 Drop 8 + SL4 + MEF)

Topics: Prism v4 - Silverlight 4
Oct 5, 2010 at 7:02 PM

So I have a module which implements IModule, and in the Initialize method I call 

_regionManager.RegisterViewWithRegion("ContentRegion", typeof(AttractorView));

This works as expected. Initializes the ModelView (presenter) class that my view imports (via MEF) and then adds the view to the ContentRegion. The problem is that I want to .Deactivate the view so it isn't shown immediately even though it's registered. RegisterViewWithRegion() doesn't specify a view name when automagically adding the view to the region so doing something like below doesn't work:

IRegion region = _regionManager.Regions["ContentRegion"];            
object view = region.GetView(typeof(viewName); //no view name was created by RegisterViewWithRegion

Do I have to go about this old school ie: the way I did in prism 2.1 and not use RegisterViewWithRegion and instead get a reference to the ModelView (presenter) via:


AttractorModelView modelView = _container.Resolve<AttractorModelView>();          
IRegion region= _regionManager.Regions["ContentRegion"];            

protected void RegisterViewsAndServices()      
     _container.RegisterType<IAttractorView, AttractorView>();      


Oct 5, 2010 at 8:47 PM


In Prism there is two techniques for UI-Composition. When you register a view in a region using the RegisterViewWithRegion method, you are using View Discovery. But when you need explicit or programmatic control over when a view is created and displayed, the recommendable approach is to use View Injection. For more information on this topic, you could read the following documentation section on MSDN:

  • UI Composition (see the When to Use View Discovery vs. View Injection section)

That said your scenario seems to require an explicit control for deactivating this view after adding this to a region. So I would recommend you to use the View Injection approach.

On the other hand, if your scenario requires to continue using View Discovery, you could opt to use Region Behaviors (RegionActiveAwareBehavior). For more information you could take a look at the following thread, where this topic has treated in detail:

Hope this helps.

Fernando Antivero

Oct 5, 2010 at 11:49 PM

Hi Fernando , Thanks for the quick reply. Looks like I'll need to use view injection. In prism 2.1 I did this via a unity container but now that I'm using MEF, are there any examples on how to do view injection with MEF?


Oct 6, 2010 at 12:10 AM

Nervermind, I figured out all I have to do is an [Import] to get a reference to my exported view via MEF.


Oct 6, 2010 at 3:26 PM


As you mentioned, you could use the Import attribute. Additionally, there is an assembly for extending MEF in Prism, so I would recommend you to use the attributes provided by this assembly (Prism.MEFExtensions). If you need more information, you could take a look at the following documentation sections in Prism4.chm:

  • Appendix C: Prism Library (see the Organization of the Prism Library section)
  • Modularity QuickStarts for WPF (updated)
  • Stock Trader Reference Implementation (updated)

For example if you need to export a module or a view, you could use:

  • ModuleExport
  • ViewExport

Additionally, if you need guidance on how your Composite Application can use MEF, you could check the following Quickstart and Reference Implementation provided in the latest drop (9) of Prism:

  • Modularity Quickstart with MEF. (Quickstarts\Modularity\Desktop\ModularityWithMef\)
  • Stock Trader Reference Implementation.

Hope this helps.

Fernando Antivero