Need a notification when a Region is ready to accept views

Topics: Prism v2 - Silverlight 3
Nov 23, 2009 at 11:34 PM

Hi,

I have a problem with my modular UI application where I need to be notified when a Region is available so that I can call AddViewToRegion/Region.Add etc. The reason I have this problem is that I am using Silverlight Navigation with Regions and calling AddViewToRegion from within my ViewModel. The problem is that there seems to be no readily available notification that I can use to determine that the region is loaded and ready to accept views. What ends up happening is that I get intermittent errors when I try to add views to the region due to the region not having been loaded yet.

Has anyone else come against this problem and solved it or found a different solution to the problem? Basically I want to add a view to a region from my ViewModel (View Injection) but I need to make sure the Region is available to accept views first.

Regards,

Mark.

Nov 24, 2009 at 3:36 AM

Update,

I've made some progress. I was thinking that in order to keep in the spirit of what Regions were designed to do, the Region itself should load the view once it (the region that is) was available. Therefore, I can use the AutoPopulateBehaviour with RegisterViewWithRegion and use the Func<object> parameter to pass my ViewModel instance to the the region (I'm using some of the concepts fron Erwin van der Valk's blog at http://blogs.msdn.com/erwinvandervalk/archive/2009/04/29/how-to-build-an-outlook-style-application-with-prism-v2-part-2.aspx).

The problem I am having now is that I only want to call RegisterViewWithRegion in my ViewModel once however there appears to be no way to remove the registration or to check if a registration has already been peformed. Does anyone have any ideas how I can overcome this without change the guidance code or an alternative suggestion to solve my problem?

Regards,

Mark.

Nov 24, 2009 at 7:03 PM

Hi Mark,

You could use the GetContents method provided by the RegionViewRegistry to get the views registered for a particular region.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Nov 25, 2009 at 3:30 AM

I ended up with two pieces of functionality to solve my problem.

1. Create PageController view that the Navigation service calls that takes care of creating a scoped UnityContainer and RegionManager and sets the RegionManager attached property on the page control. The page controller then creates the ViewModel in the scoped container (and hence I don't get region name conflicts by navigating to the same page more than once).

2. I create a custom behaviour that inherits from IHostAwareRegionBehaviour that checks that the host datacontext object (ViewModel) implements IRegionManagerAware. If it does, it calls OnRegionAttach and send through the region name as a parameter. My ViewModel can then listen for a particular region and safely load views into it.

All this is just a variation on post I found although finding IHostAwareRegionBehaviour was the key to getting this all to work.

Thanks,

Mark.