为什么我会收到此错误"The current type,..., is an interface and cannot be constructed. Are you missing a type m



我正在使用Prism 6.3,Unity Container 4,WPF。我得到的错误是"当前类型...是一个接口,无法构建..."当我尝试解析一个类时,但在我看来我已经正确映射了所有内容。我以前用过这个,没有问题...我很困惑。

我有一个模块,其中包含一个名为ICurrentWorkingEmail的接口,该接口映射到CurrentWorkingModel类。我创建了模块类 (IModule) BizCoreModule,其中这些类型在容器中注册。

public class BizCoreModule : IModule
{
private IUnityContainer _container;
public BizCoreModule(IUnityContainer container)
{
_container = container;
}
public void Initialize()
{
_container.RegisterType<ICurrentWorkingEmail, CurrentWorkingEmail>(new ContainerControlledLifetimeManager());
}
}

在引导程序中,我已经将模块注册到模块目录。请注意,似乎有不同的方法可以做到这一点,并且已经尝试过,但似乎没有区别......我坚持使用过去使用的东西。

protected override IModuleCatalog CreateModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(DataLayerModule));
catalog.AddModule(typeof(BizCoreModule));
return catalog;
}

调试时,我一直在将容器注入视图模型并尝试解析构造器中的 CurrentWorkingEmail 类(通常不会这样做)。

private IUnityContainer _container;
public SearchViewModel(IUnityContainer container)
{
_container = container;
var currentWorkingEmail = _container.Resolve<ICurrentWorkingEmail>();
}

错误发生在_container上。解析()

Haukinger指出了这个问题。我试图在目录启动之前解决ICurrentWorkingEmail。我没有意识到 shell 会在目录启动之前加载......我试图在shell的视图模型中使用CurrentWorkingEmail。为了解决这个问题,我将 shell 组件移动到它自己的模块中,并使用区域管理器注入它。

相关内容

最新更新