Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何使用PRISM 4.1和;团结一致_Wpf_Silverlight_Mvvm_Unity Container_Prism 4 - Fatal编程技术网

Wpf 如何使用PRISM 4.1和;团结一致

Wpf 如何使用PRISM 4.1和;团结一致,wpf,silverlight,mvvm,unity-container,prism-4,Wpf,Silverlight,Mvvm,Unity Container,Prism 4,我正在使用PRISM 4.1和Unity编写一个WPF应用程序。应用程序将具有可配置的每个UI组件,包括Shell本身 例如,我有一个名为IShell的接口。如果应用程序的消费者对我的默认实现不满意,他们可以拥有自己的IShell实现,该默认实现以某种固定方式定义了区域和视图 现在,从我的Bootstrapper类(它继承了UnityBootTrapper)中,我想知道使用Unity容器为IShell注册的类型。被重写的CreateShell将返回IShell的配置类型 我的App.config

我正在使用PRISM 4.1和Unity编写一个WPF应用程序。应用程序将具有可配置的每个UI组件,包括Shell本身

例如,我有一个名为
IShell
的接口。如果应用程序的消费者对我的默认实现不满意,他们可以拥有自己的
IShell
实现,该默认实现以某种固定方式定义了区域和视图

现在,从我的Bootstrapper类(它继承了UnityBootTrapper)中,我想知道使用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>
但是,当我运行此代码时,会出现异常:“InvalidOperationException-当前类型Interfaces.IShell是一个接口,无法构造。是否缺少类型映射?”

如何解决这个问题?
为什么应用程序无法知道app.config文件中存在的IShell的注册类型?

是否应该改用UnityBootTrapper的容器来解析shell

var obj =  Container.Resolve<IShell>() as DependencyObject;
return obj;
var obj=Container.Resolve()作为DependencyObject;
返回obj;

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

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

我必须在我的
PrismAppBootstrapper
类中重写其他方法以及
CreateShell

例如

public类PrismAppBootstrapper:UnityBootstrapper
{
受保护的覆盖IModuleCatalog CreateModuleCatalog()
{
ModuleCatalog catalog=新配置ModuleCatalog();
退货目录;
}
受保护的覆盖无效配置容器()
{
base.ConfigureContainer();
UnityConfiguration部分配置部分=
(UnityConfiguration节)ConfigurationManager.GetSection(“unity”);
if(configurationSection!=null)
{
configurationSection.Configure(此.Container);
}
}
受保护的覆盖依赖对象CreateShell()
{
IShell shell=this.Container.TryResolve();
返回外壳作为窗口;
}
受保护的覆盖无效初始值设置Shell()
{
base.InitializeShell();
Application.Current.MainWindow=(Window)this.Shell;
Application.Current.MainWindow.Show();
}
}

我已经试过了。
Resolve
的重载不再可用:(您需要引用UnityExtensions:使用Microsoft.Practices.Prism.UnityExtensions;
 PrismAppBootstrapper prismAppBootstrapper = new PrismAppBootstrapper();
        prismAppBootstrapper.Run();
var obj =  Container.Resolve<IShell>() as DependencyObject;
return obj;
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();
        }
    }