Prism RegionManager和Unity容器的问题 [英] Problem with Prism RegionManager and Unity container

查看:363
本文介绍了Prism RegionManager和Unity容器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要的Silverilght项目和其他模块,我正在使用Prism将它们与Unity一起用于依赖注入.

我有一个模块依赖于IRegionManager,而这个模块自然会被Unity注入到其中.

我要解决的问题是,即使我在Shell中声明了两个区域,我得到的RegionManagerRegions属性也不包含任何区域:

regions:RegionManager.RegionName="MainRegion"

我无法弄清楚这种现象的原因.我试过使用

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

RegionManager注册为单例

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

以及让团结一致来处理这个问题.但是,我得到了相同的结果:(

我想要实现的是将视图注入特定区域并激活它,这是无法使用

完成的

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

这就是为什么我需要掌握"MainRegion"对象本身并对其进行操作的原因.

我希望有人能解决这个问题.我将非常感激:)

修改: 我应该提到,视图发现可以很好地工作,可以RegisterViewWithRegion,但是当我检查RegionManagerRegions属性时,我发现它为空!

解决方案

在思考此问题及其发生的上下文时,令我震惊的是,就像软件中的大多数错误一样,问题不在我要找的地方!

我的设置如下:我从Silverlight业务应用程序项目模板开始.这给了我一个MainPage,用作我的Shell,还有几个视图(HomeAbout).现在,我不再将区域放在我的Shell上,而是将它们放在Home视图中,将MainPage保留为母版页.

我对此的理解是Prism为Shell设置了一个RegionManager,并将其传递给需要它的任何人(使用依赖注入容器).这样,当我在模块中要求一个IRegionManager时,我得到了一个Shell,它实际上不包含任何区域(因此为空的Regions属性).

解决,我要做的就是将我的区域放在正确的位置:壳牌,并且现在一切正常.

更新: 似乎我的接受"实际上是正确的:),这摘录自原始文档 :(在作用域内下)

使用附加属性定义区域的视图将自动继承其父级的RegionManager.通常,这是在 shell 窗口中注册的全局RegionManager

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!

解决方案

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

这篇关于Prism RegionManager和Unity容器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆