Quantcast
Channel: patterns & practices: Prism
Viewing all articles
Browse latest Browse all 1878

Commented Issue: scpoed unity container and RegionManager.RegisterViewWithRegion [9693]

$
0
0
I have a multi window wpf app, I create scoped container in each window's viewmodel and inject some window scoped service object into the container, then I use this unity container to resolve views and inject into the window. but in some module, the view have a region host inside and there have some vew registertion in other module. this cause problem, the sub view which has been registered in to view regestery by using RegionManager.RegisterViewWithRegion(regionname, viewtype) will be resolved by a global servicelocator. so the window scoped service object ca not been reterived by thosr viewmodel's cotr
Comments: ** Comment from web user: aadami **

Hi,

Based on my understanding when using the view discovery approach to register your views with an specific region, if you want to specify that the view will be resolved from a different container, I believe you could benefit of the RegisterViewWithRegion overload method that allows you to pass a delegate that can be used to specify how to retrieve the content associated with the region name:

void RegisterViewWithRegion(string regionName, Func<object> getContentDelegate);

As you mentioned, by default a delegate which uses the global ServiceLocator is created. This can be seen in the RegionViewRegistry class provided with Prism. Hence in order to specify that a different container should be used to resolve the desired type, you could create a custom delegate which uses your child container instead (e.g. regionViewRegistry.RegisterViewWithRegion("innerRegion", () => childContainer.Resolve<MyView>());)

The problem with this approach, is that you will have to define some logic to pass the corresponding child container that will be used in the module to register the views. Perhaps, a possible approach could be to use the event aggregator to communicate between these modules. For example, your view model class that contains a region which should be populated by views in other modules, could publish this event passing the name of the region and the corresponding child container as a payload. Then the subscriber in your module could register its views with the child container passed as mentioned above.

I hope this helps,

Agustin Adami
http://blogs.southworks.net/aadami


Viewing all articles
Browse latest Browse all 1878

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>