【发布时间】:2013-12-05 11:40:50
【问题描述】:
我正在使用 PRISM 4.1 和 Unity 编写 WPF 应用程序。该应用程序的每个 UI 组件都可配置,包括 Shell 本身!
例如我有一个名为IShell 的接口。如果应用程序的消费者对我的默认实现不满意,他们可以拥有自己的IShell 实现,该实现具有以某种固定方式定义的区域和视图。
现在,从我的 Bootstrapper 类(继承 UnityBootstrapper),我想知道使用 Unity 容器为 IShell 注册的类型。覆盖的CreateShell 将返回IShell 的配置类型。
我的App.config 看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity>
<container>
<register type="Interfaces.IShell, Interfaces" mapTo="PrismApp.Shell, PrismApp"/>
</container>
</unity>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
在 Bootstrapper 类中,我有以下代码:
public class PrismAppBootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
var obj = ServiceLocator.Current.GetInstance<IShell>() as DependencyObject;
return obj;
}
}
在 WPF 应用程序的 App.xaml.cs 中,我正在实例化PrismAppBootstrapper:
PrismAppBootstrapper prismAppBootstrapper = new PrismAppBootstrapper();
prismAppBootstrapper.Run();
但是,当我运行此代码时,出现异常:“InvalidOperationException - 当前类型 Interfaces.IShell 是一个接口,无法构造。您是否缺少类型映射?”
如何解决这个问题? 为什么应用程序不知道 app.config 文件中的 IShell 注册类型?
【问题讨论】:
标签: wpf silverlight mvvm unity-container prism-4