MEF:尽管已发现并加载了PRISM模块,但应用程序声称无法找到它们

时间:2011-08-07 09:28:47

标签: c# .net prism mef

我在PRISM 4.0应用程序中使用MEF来加载模块。 为了确保它们被下载,我已经让我的Shell导入了IPartImportsSatisfiedNotification。然后在OnImportSatirsfied()方法中,我可以在调试器中清楚地看到找到了两个模块。 (见下面的截图)

enter image description here

但是我不断收到此错误消息:

  

无法找到具有类型的模块   “SalesContactManagement.Modules.NavigationModule.NavigationModule,   SalesContactManagement.Modules.NavigationModule,Version = 1.0.0.0,   导出的模块中的Culture = neutral,PublicToken = null'。使   确保模块目录中的模块名称与指定的模块名称匹配   模块类型的ModuleExportAttribute。

知道为什么MEF不起作用?任何帮助都非常感谢。

更新

当我将NavigationModule清空到最低限度时,它很有趣。它可以正常工作。

 [ModuleExport(typeof(NavigationModule))]
    public class NavigationModule : IModule
    {
        private readonly IRegionManager _regionManager;
        private readonly ToolbarViewModel _toolbarViewModel;

        public void Initialize()
        {

        }

        //[ImportingConstructor]
        //public NavigationModule(RegionManager regionManager)
        //{
        //    //_toolbarViewModel = toolbarViewModel;
        //    _regionManager = regionManager;
        //}
}

但是只要我在那里放置一个ImportingContructor,对于已在Bootstrapper中注册的类型,它就会失败。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我没有对Prism做过任何事情,但导出的IRegionManager类型是什么?您的导入构造函数目前是:

[ImportingConstructor]
public NavigationModule(RegionManager regionManager) { }

然而,应该是:

[ImportingConstructor]
public NavigationModule(IRegionManager regionManager) { }

注意类RegionManager和接口IRegionManager之间的区别作为构造函数参数。

修改:供您发表评论。如果您想每次都启动一个新实例,可以使用PartCreationPolicyAttribute

[Export(typeof(ISomething)), PartCreationPolicy(CreationPolicy.NonShared)]

或者,您可以使用ExportFactory,例如:

[Import] ExportFactory<ISomething> SomethingFactory { get; set; }

答案 1 :(得分:0)

我建议使用fusion log viewer来查找模块的加载方式。安装Visual Studio时应该为您安装Fusion日志查看器(您应该只需点击start + fusion来搜索它)

可能的问题:

  1. 版本不匹配
  2. 强名称不匹配
  3. LoadContext不匹配
  4. 其他东西
  5. Fusion Log Viewer可能会帮助您查明错误。