未加载MEF模块或尝试“强制”加载时出错

时间:2015-04-20 15:35:32

标签: c# prism mef

“ConfigureAggregateCatalog”与“ConfigureModuleCatalog”有何不同? mef上的prism 5.0快速入门示例显示了3种加载模块的方法:从程序集目录,目录编目和配置文件。看起来AggregateCatalog.Catalogs.Add(new AssemblyCatalog(...))足以查找和加载模块。没有ConfigureModuleCatalog的ModuleCatalog.Add(new ModuleInfo(...),我甚至无法得到任何错误;模块未加载。

以下面显示的方式加载模块时出现以下异常。

Microsoft.Practices.Prism.Composition.dll中出现未处理的“Microsoft.Practices.Prism.Modularity.ModuleTypeLoadingException”类型异常

其他信息:无法加载模块ExportBuildPreferencesModule的类型。

错误是:无法在导出的模块中找到类型为“FourZero.ExportBuild.Preferences.ExportBuildPreferencesModule,FourZero.ExportBuild.Preferences,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”的模块。确保模块目录中的模块名称与ModuleExportAttribute上为模块类型指定的模块名称相同。

如果我删除了ConfigureModuleCatalog方法,我会得到:Additional information: Module ExportBuildPreferencesModule was not found in the catalog.

public class BootStrapper : MefBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return this.Container.GetExportedValue<MainWindow>("MainWindow");
    }

    protected override void InitializeModules()
      {
          this.ModuleCatalog.Initialize();
      }

    protected override void InitializeShell()
    {
        base.InitializeShell();
        Application.Current.MainWindow = (Window)this.Shell;
        Application.Current.MainWindow.Show();
        this.moduleManager = this.Container.GetExportedValue<IModuleManager>();
        this.moduleManager.LoadModule("ExportBuildPreferencesModule");
        //this.moduleManager;
    }

    private IModuleManager moduleManager;

    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();
        //get the exported Shell from the assembly containing the bootstrapper (this) (MainWindow)
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));


        //get the exports from the Preferences assembly
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ExportBuildPreferencesModule).Assembly));
    }


    protected override void ConfigureModuleCatalog()
    {

         base.ConfigureModuleCatalog();
         string path = @"G:\ExportBuild\FourZero.ExportBuild\FourZero.ExportBuild.Main\bin\Debug\FourZero.ExportBuild.Preferences.dll";
         Type modulePreferencesType = typeof (ExportBuildPreferencesModule);
         this.ModuleCatalog.AddModule(new ModuleInfo()
                                     {
                                         ModuleName   = modulePreferencesType.Name,
                                         ModuleType = modulePreferencesType.AssemblyQualifiedName,
                                         Ref = new Uri(path, UriKind.RelativeOrAbsolute).AbsoluteUri,
                                         InitializationMode = InitializationMode.WhenAvailable

                                     });            
    }
}

以下是导出模块的类。更改为[ModuleExport("ExportBuildPreferenceModule", typeof(ExportBuildPreferencesModule))]会产生相同的错误。

[ModuleExport( typeof(ExportBuildPreferencesModule))]
public class ExportBuildPreferencesModule : IModule
{
    //[Import(AllowRecomposition = false)] 
    private readonly IRegionManager _regionManager;

    [ImportingConstructor]
    public ExportBuildPreferencesModule(IRegionManager inRegionManager)
    {
        this._regionManager = inRegionManager;
    }

    [Import]
    private ExportBuildPreferencesView _exportBuildPreferencesView;
    public void Initialize()
    {
         this._regionManager.RegisterViewWithRegion(RegionNames.ExportBuildPreferencesRegion,
         typeof (ExportBuildPreferencesView));
    }
}

0 个答案:

没有答案