Problem with Prism RegionManager and Unity container

别等时光非礼了梦想. 提交于 2019-12-11 07:51:26

问题


I have a main Silverilght project and other modules and I'm using Prism to glue them together along with Unity for Dependency Injection.

I have one of my modules that depends on an IRegionManager which, naturally, gets injected to it by Unity.

The issue I'm running to is that, the Regions property of the RegionManager that I get doesn't contain any regions even though I have declared two of them in my Shell as follows:

regions:RegionManager.RegionName="MainRegion"

I can't figure out the reason of this behavior. I've tried registering a RegionManager as a singleton using

Container.RegisterInstance<IRegionManager>(new RegionManager());

as well as leaving unity handle this. However, I got the same results :(

what I'm trying to achieve is to inject a view into a particular region and activate it, which can't be done using

_regionManager.RegisterViewWithRegion("MainRegion", typeof(MyView));

that's why I need to get hold of the "MainRegion" object itself and manipulate it.

I hope somebody has this figured out. I would be very thankful :)

Edit: I should mention that the view discovery works just fine, I can RegisterViewWithRegion, but when I check the Regions property of the RegionManager, I find it empty!


回答1:


while thinking about this problem and the context in which it happened, it striked me that, like most bugs in software, the problem wasn't where I was looking!

My setup was as follows: I started out with the Silverlight Business Application project template. this gave me a MainPage which I used as my Shell, and a couple of views(Home and About). Now, instead of having the regions sit on my Shell, I put them in the Home view, keeping the MainPage as kind of a master page.

My take on this is that Prism sets a RegionManager for the Shell, and passes it around to whoever needs it (using a Dependency Injection Container). This way, when I asked for an IRegionManager in my module, I got the one for my Shell which doesn't really contain any region (hence the empty Regions property).

To solve this, all I had to do was put my regions in their rightfull place: the Shell, and all is working well (for) now.

Update: Seems like my "take on this" is actually correct :), this is an excerpt from the Prism Documentation: (under Scoped Regions)

Views defining regions with attached properties automatically inherit their parent's RegionManager. Usually, this is the global RegionManager that is registered in the shell window



来源:https://stackoverflow.com/questions/5332466/problem-with-prism-regionmanager-and-unity-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!