我想了解一些事情。
我有“Service”silverlight应用程序项目,其中包含模块类和进入其“Initialize”方法的下一个代码行,以便注册类型:
unityContainer.RegisterInstance<IService>(instance);
我还有另一个“Controls”silverlight 应用程序项目。模块在“catalog.xaml”文件中描述:
<Modularity:ModuleInfo
Ref="Service.xap"
ModuleName="ServiceModule"
ModuleType="Service.ModuleDefinitions.Module, Service, Version=1.0.0.0">
</Modularity:ModuleInfo>
您可以在下面找到“Bootstrapper”代码:
protected override IModuleCatalog CreateModuleCatalog()
{
Uri uri = new Uri("catalog.xaml", UriKind.Relative);
ModuleCatalog moduleCatalog = Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(uri);
// NOTE: State of "ServiceModule" is "NotStarted" here...
return moduleCatalog;
}
protected override DependencyObject CreateShell()
{
Shell shell = new Shell();
Application.Current.RootVisual = shell;
IRegionManager regionManager = Container.Resolve<IRegionManager>();
regionManager.RegisterViewWithRegion("MainRegion", typeof(MainView));
return shell;
}
一个 ViewModel 类(进入“Controls”项目)包含对 IService 接口(interface)的引用,该接口(interface)必须注册到“Service”项目的 Modul 类中。
在程序调试期间,CreateModuleCatalog() 和 CreateShell() 方法执行没有任何问题,但在执行注册 IService 的 Initialize() 方法之前出现异常。
错误消息:“当前类型 Service.ServiceReference.IService 是一个接口(interface),无法构造。您是否缺少类型映射?”
我认为我只需要注册/映射类型一次,如果我已经将其完成到服务项目的模块类中,那么我就不必再次进行控制项目了。
我需要帮助来理解上述情况:)
最佳答案
您确定在尝试从容器解析对象之前执行此代码吗?
unityContainer.RegisterInstance<IService>(instance);
如上所述here ,使用带有接口(interface)的 RegisterInstance 方法会自动建立映射,因此无需您显式执行此操作。
但是,如果您尝试解析没有 type mapping 的接口(interface)首先建立你会得到你所得到的错误,因为没有办法实例化接口(interface)。
关于.net - Prism 。使用 RegisterInstance 执行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4465676/