2013-03-20 51 views
0

我有一个非常基本的棱镜应用程序,我试图用它作为一个大项目的开始。我有一个定义为“MainContent”的单个区域的shell。项目中有3个模块。 MainMenuModule,MovieModule和TVModule。电影和电视模块依赖于MainMenuModule。模块使用DirectoryModuleCatalog加载。当包含TVModule时会出现该问题,更具体地说,当调用TVModule的构造函数时会引发异常。如果我改变构造函数中我没有得到任何异常,TVModule负载预期以下WPF Prism模块错误解决IUnityContainer

public TVModule(IUnityContainer container, IRegionManager manager) 
{ 
    _container = container; 
    _manager = manager; 
} 

:构造下面将抛出一个错误。

public TVModule(IRegionManager manager) 
{ 
    _manager = manager; 
} 

当Unity试图解析IUnityContainer时,我得到的异常被抛出。令我困惑的是,MovieModule的构造器与TVModule的构造器完全相同,而且没有问题。例外细节。

{"Resolution of the dependency failed, type = \"TVModule.TVModule\", name = \"(none)\". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, Microsoft.Practices.Unity.IUnityContainer, is an interface and cannot be constructed. Are you missing a type mapping? At the time of the exception, the container was:

Resolving TVModule.TVModule,(none) Resolving parameter \"container\" of constructor TVModule.TVModule(Microsoft.Practices.Unity.IUnityContainer container, Microsoft.Practices.Prism.Regions.IRegionManager manager) Resolving Microsoft.Practices.Unity.IUnityContainer,(none) "}

{"The current type, Microsoft.Practices.Unity.IUnityContainer, is an interface and cannot be constructed. Are you missing a type mapping?"}

编辑:我已经上传我的源代码拷贝到收存箱here

+0

您是否注册了两个模块到UnityContainer?与“TVModule”相比,“MovieModule”的设置/注册方式有什么不同? – ryrich 2013-03-20 18:17:23

+0

除了形成MovieModule之外,没有什么不同之处,它有一个额外的视图,即TVModule没有。随意从我添加到我的问题的保管箱链接下载我的源代码。 – OriginalMoose 2013-03-20 18:47:14

+1

这不是一个答案,而是一个很好的建议:你应该避免以明确的方式在类之间传递ioc容器。它是支持解析服务的容器,但容器本身不是商业服务。传递一个容器使得你的类依赖于不好的容器 - ioc应该帮助你而不是破坏你的代码。 – 2013-03-20 18:51:52

回答

0

从Wiktor的的建议,我已删除了经过统一容器到模块的构造。我现在使用servicelocator在需要时访问容器。

ServiceLocator.Current.GetInstance<IUnityContainer>(); 
相关问题