Delayed instantion of registered Views with Region

Topics: Prism v2 - WPF 4
Jun 10, 2010 at 5:18 PM

Hi all,

a quick question about how you would do that. I try to have some sort of lacy loading where the views only get created once the region is actually loaded into the visual tree. Is that possible at all with PRISM (thinking of some kind of Regionbehavior) or would I have to go outside Prism and do it seperatly which would be a shame.



Jun 11, 2010 at 10:10 PM

Hi Cristoph,

If you use View Discovery to register the views into regions, they will be created and displayed only when the region is created. You can find more information about View Discovery in this article.

The scenario that you are describing is possible to achieve because of the DelayedRegionCreationBehavior, which is attached to regions by default. You can find it in the the Behaviors folder in the Regions folder inside the Composite.Presentation project of the CAL. That behavior creates the IRegion only when the control that will host it is actually loaded into the visual tree.

I hope you find this helpful.

Guido Leandro Maliandi

Jun 14, 2010 at 10:37 AM

Hi Guido,

thanks for the reply. I tried the described approach and it works nicely after some changes.

However, there is one open question left: Is there any way I can remove the registered view again? Using the "view injection" method I am able to simply call Region.Remove(object view) and the view is removed from the UI. However, using "view discovery" I cannot guarantee that the view has already been added to the region and therefore want to remove the registered view from the region before it gets added once the region gets displayed.


Jul 7, 2010 at 7:02 PM

Hi Christoph,

As mentioned in this thread, view discovery isn’t designed for when you need to have programmatic control over when you add or remove views. However, there are some possibilities for achieving your scenario.

One of these would be to iterate through the views assigned to a region, and delete one that fulfills certain criteria. That would be possible since, even if you use the RegionViewRegistry to register views with regions following the View Discovery approach, you can also access a region through the RegionManager. The code for that would look like this (assuming that you have already created the region named MyRegion):

IRegion region = this.regionManager.Regions[“MyRegion”];
foreach (var view in region.Views)

In case you want to remove the registered view from the region before the region is displayed, take into account that Prism offers no possibility to unregister a view from a region in the RegionViewRegistry out of the box, so you would have to wait for the region to be added to the visual tree in order to remove views from it. If you need to do so, you would have to modify the RegionViewRegistry or the AutoPopulateBehavior to support this (for example, adding an UnregisterViewWithRegion method to the RegionViewRegistry).

I hope you find this helpful.

Guido Leandro Maliandi