.net - Prism 。使用 RegisterInstance 执行任务

标签 .net silverlight-4.0 c#-4.0 unity-container prism

我想了解一些事情。

我有“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/

相关文章:

本地计算机上多个进程的 SQL CE 文件共享冲突

c# - InputScope 类型 NumericPassword 在我的情况下不起作用

c# - Autofac 无法解析外部 Controller 上的属性

c# - 如果我需要使用一个小表来部署应用程序,应该使用哪个数据库?

c# - 如何通过 Linq 过滤一对多关系的查询

c# - 更新图表中的 y 轴最大值

c# - 转换为值类型 'Int32' 失败,因为具体化值为 null

silverlight-4.0 - MVVM 和导航服务

silverlight-4.0 - Silverlight 4 数据网格打印

c# - 如何从它的扩展方法修改类?