WPF+PRISM+MEF初始化DownloadedPartCatalogCollection

WPF+PRISM+MEF initializing DownloadedPartCatalogCollection

我正在尝试从我的 MefBootStrapper 实现中初始化一个模块

Type type = typeof(OrderDetailsModule.OrderDetailsModule);

ModuleInfo mi = new ModuleInfo {
    ModuleName = type.Name,
    Ref = new Uri(type.Assembly.Location, UriKind.RelativeOrAbsolute).AbsoluteUri,
        InitializationMode =InitializationMode.WhenAvailable,
        ModuleType = type.AssemblyQualifiedName
    };

this.ModuleCatalog.AddModule(mi);

我收到一个错误

failed to load type for module OrderDetailsModule. \r\nError was: Unable to locate the module with type 'OrderDetailsModule.OrderDetailsModule, OrderDetailsModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' among the exported modules. Make sure the module name in the module catalog matches that specified on ModuleExportAttribute for the module type.."

挖掘Prism,在MefModuleInitializer中,有一个if (this.downloadedPartCatalogs.TryGet(moduleInfo, out partCatalog)),下载的部分是空的。我可以在 MefModuleInitializer class 中看到,downloadedPartCatalogs 是通过 ImportingConstructor 属性注入的。

这是我的 OrderDetailsModule class

[Export("OrderDetailsModule")]
public class OrderDetailsModule
{
}

问题是,我在哪里导出下载的PartCatalogs?

您的模块 class 必须实现 IModule 接口并使用 ModuleExportAttribute 进行属性化。

[ModuleExport(typeof(ModuleD))]
public class ModuleD : IModule 
{...}

或者使用 AssemblyCatalog 自动发现和加载模块。

protected override void ConfigureAggregateCatalog()
{
    base.ConfigureAggregateCatalog();

    this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA).Assembly));
    this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleC).Assembly));
    . . .
}

请阅读 Prism 文档:https://github.com/PrismLibrary/Prism/blob/master/Documentation/WPF/30-ModularApplicationDevelopment.md#modules-in-mef