MEF2 和 Caliburn.Micro - IWindowManager

我无法让 MEF2 将 WindowManager 或 EventAggregator 导入我的 ShellViewModel。我自己的所有课程似乎都运行良好。

我已经将我的项目设置为使用 MEF2(System.ComponentModel.Composition 和子项)。我按照Customizing The Bootstrapper文档以及 Tim Corey 的From Zero to Proficient with MEF开始,并意识到这些是 MEF1。我阅读了MEF 2 Preview Beginners GuideManaged Extensibility Framework Improvements in .NET 4.5,将 CompositionBatch 替换为 RegistrationBuilder,并删除了建议的类和属性属性,以支持 RegistrationBuilder 的 Fluid API 来配置导入和导出。

当它尝试使用 _eventAggregator 时,我在 ShellViewModel 的 OnActivate 覆盖中得到一个 NullReferenceException。从未进行过进口。

如果我通过注释掉 OnActivate() 和 OnDeactivate() 来运行它,它会启动并显示一个空白窗口,因此它会正确加载 shell。它只是没有导入任何依赖项。

这是最简单的 Bootstrapper 和 ViewModel 来显示问题。

引导程序.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows;

using Caliburn.Micro;

using System.ComponentModel.Composition;

using System.ComponentModel.Composition.Hosting;

using System.ComponentModel.Composition.Registration;

using System.Reflection;

using MEF2Test.ViewModels;


namespace MEF2Test

{

    public class MefBootstrapper : BootstrapperBase

    {

        private CompositionContainer _container;


        public MefBootstrapper()

        {

            Initialize();

        }


        protected override void Configure()

        {

            RegistrationBuilder cmBuilder = new RegistrationBuilder();

            RegistrationBuilder cmpBuilder = new RegistrationBuilder();

            RegistrationBuilder vmBuilder = new RegistrationBuilder();


            cmBuilder.ForTypesDerivedFrom<IEventAggregator>().Export<IEventAggregator>();

            cmpBuilder.ForTypesDerivedFrom<IWindowManager>().Export<IWindowManager>();

            vmBuilder.ForTypesDerivedFrom<IShell>().Export<IShell>();


        }



慕尼黑8549860
浏览 116回答 1
1回答

幕布斯6054654

我想通了。出于某种原因,我必须使用:ForType<ConcreteType>.ImportProperty<Interface>(x&nbsp;=>&nbsp;x.PublicPropertyToSetInterface);注册进口时。这使我的简单示例起作用,从而回答了我的问题。我的实际项目仍然存在System.InvalidCastException:&nbsp;'Unable&nbsp;to&nbsp;cast&nbsp;object&nbsp;of&nbsp;type&nbsp;'System.Lazy`1[System.Object]'&nbsp;to&nbsp;type&nbsp;'Caliburn.Micro.IWindowManager'.'错误,但这可能是我在注册时遗漏或歪曲的内容。
打开App,查看更多内容
随时随地看视频慕课网APP