【问题标题】:MEF: Despite having found and loaded the PRISM modules, app claims they can't be foundMEF:尽管找到并加载了 PRISM 模块,但应用程序声称找不到它们
【发布时间】:2011-08-07 09:28:46
【问题描述】:

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

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

无法找到具有类型的模块 'SalesContactManagement.Modules.NavigationModule.NavigationModule, SalesContactManagement.Modules.NavigationModule,版本=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 中注册的类型,它就会失败。有什么想法吗?

【问题讨论】:

    标签: c# .net prism mef


    【解决方案1】:

    我没有对 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; }
    

    【讨论】:

    • 谢谢马修,我终于解决了。调试 MEF 比 Unity 困难得多。但它很有趣。这是被拒绝的进口产品之一,因此它拒绝了包括模块在内的整个链条。这让我相信没有找到该模块。我喜欢MEF。让我们看看。我还有一个问题,你知道我如何强制 MEF 获取一个新实例吗?我不断收到相同的实例,即使它被定义为非共享......也许我应该提出一个新问题......
    【解决方案2】:

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

    可能的问题:

    1. 版本不匹配
    2. 强名称不匹配
    3. Lo​​adContext 不匹配
    4. 别的东西

    Fusion Log Viewer 或许可以帮助您查明错误。

    【讨论】:

    • 感谢您的提示。我已经启动了它,但前三个按钮是灰色的。我必须在上面拖放一个日志文件吗?
    • 您必须以管理员身份运行它。另请注意,您必须在使用后将其设置为否则融合加载程序将继续记录(损害性能并占用磁盘空间)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2018-05-27
    • 2016-11-01
    • 2021-10-11
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多