views:

916

answers:

2

In Composite WPF (Prism), when adding modules to the IRegionManger collection, what is the difference between using IRegion.Add and IRegionManager.RegisterViewWithRegion?

IRegion.Add

public void Initialize()
{
    _regionManager.Regions["MainRegion"].Add( new ModuleAView() );
}

IRegionManager.RegisterViewWithRegion

public void Initialize()
{
    _regionManager.RegisterViewWithRegion( "MainRegion", typeof( ModuleAView ) );
}
A: 

RegisterViewWithRegion raises the OnContentRegistered event, but of course that could not be the case depending on your DI

slf
Can you elaborate? This isn't clear.
Anderson Imes
when you directly modify the property with the bracket operator, you aren't going to get the event raised, but when you call the other method, you get both the modified collection, and an event that can notify anyone listening to it
slf
+2  A: 

The difference is who is responsible for creating the view. In the IRegion.Add scenario (also called View Injection) you are responsible for instantiating the view beforehand. In the other scenario with RegisterViewWithRegion (also called View Discovery), the region manager instantiates the view itself.

There are some technical reasons you would want to do one or the other. For example

  • you had a more complicated way of creating views (maybe you want to create the View and its ViewModel and marry them by setting the DataContext property yourself), you'd need to use View Injection
  • if you take advantage of Region Scopes, you will be forced to use View Injection.

The relevant documenation is: For View Composition (including View Injection vs. View Discovery and discussions of View-First or View-Presenter-First approaches): http://msdn.microsoft.com/en-us/library/dd458944.aspx

There's also a really handy "when to use each" section. Here's the excerpt from the docs:

  • Explicit or programmatic control over when a view is created and displayed, or when you need to remove a view from a region, for example, as a result of application logic.
  • To display multiple instances of the same views into a region, where each view instance is bound to different data.
  • To control which instance of a region a view is added (for example, if you want to add customer detail view to a specific customer detail region). Note that this scenario requires scoped regions described later in this topic.

Hope this helps.

Anderson Imes
@Anderson - your answer does indeed help. My understanding is that IRegion.Add and IRegionManager.RegisterViewWithRegion both create a concrete instance of the view and immediately adds it to the IRegionCollection. Admittedly, I'm just getting a foothold on CAG. Could you point me to an example with more detail? I've gone through the QuickStarts and HowTos, but I'm not exactly grasping when to one or the other because the samples are using them in the Imodule.Initialize method and neither appear to do anything different in the examples.
Metro Smurf
No, the difference is with Add, you are passing in an already instantiated view. With RegisterViewWithRegion, you pass in either a Type, a Delegate, OR an Object (instantiated view). Most of the time, the latter overload isn't used. Give me a second and I'll point you at the right place in the Docs.
Anderson Imes
Updated with a link to the best resource in the documentation and I pulled out the official verbiage for when to use view injection vs. view discovery. Hopefully it's clearer than my description.
Anderson Imes
@Anderson - I'll spend some time this afternoon re-reading the UI Composition docs. fwiw: my assumption of both methods creating a concrete instance of the view immediately to the IRegionCollection was based on stepping through the debugger and towards the end of the RegisterViewWithRegion cycle, a concrete instance of the view was created and added to the collection. Thanks so much for your help!
Metro Smurf
That's always going to happen of course. Its just what code is responsible for the instantiating... yours or theirs :)
Anderson Imes

related questions