【问题标题】:How to keep the Shell configurable with PRISM 4.1 & Unity?如何使用 PRISM 4.1 和 Unity 保持 Shell 可配置?
【发布时间】: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


    【解决方案1】:

    使用 Unity 容器可以保持 Shell 可配置。

    问题是我的 Unity 容器未正确配置为从 app.config 文件读取映射/注册。所以它在运行时无法知道IShell 的映射。

    我必须在 PrismAppBootstrapper 类中覆盖其他方法以及 CreateShell

    例如

    public class PrismAppBootstrapper : UnityBootstrapper
        {
            protected override IModuleCatalog CreateModuleCatalog()
            {
                ModuleCatalog catalog = new ConfigurationModuleCatalog();
                return catalog;
            }
    
            protected override void ConfigureContainer()
            {
                base.ConfigureContainer();
                UnityConfigurationSection configurationSection =
                    (UnityConfigurationSection) ConfigurationManager.GetSection("unity");
                if (configurationSection != null)
                {
                    configurationSection.Configure(this.Container);
                }
            }
    
            protected override DependencyObject CreateShell()
            {
                IShell shell = this.Container.TryResolve<IShell>();
                return shell as Window;
            }
    
            protected override void InitializeShell()
            {
                base.InitializeShell();
    
                Application.Current.MainWindow = (Window)this.Shell;
                Application.Current.MainWindow.Show();
            }
        }
    

    【讨论】:

      【解决方案2】:

      您是否应该改用 UnityBootstrapper 的容器来解析您的 shell?

      var obj =  Container.Resolve<IShell>() as DependencyObject;
      return obj;
      

      【讨论】:

      • 我已经试过了。 Resolve 的重载不再可用:(
      • 你需要参考UnityExtensions: using Microsoft.Practices.Prism.UnityExtensions;
      猜你喜欢
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 2023-03-09
      • 2016-04-28
      相关资源
      最近更新 更多