我无法让 MEF2 将 WindowManager 或 EventAggregator 导入我的 ShellViewModel。我自己的所有课程似乎都运行良好。我已经将我的项目设置为使用 MEF2(System.ComponentModel.Composition 和子项)。我按照Customizing The Bootstrapper文档以及 Tim Corey 的From Zero to Proficient with MEF开始,并意识到这些是 MEF1。我阅读了MEF 2 Preview Beginners Guide和Managed 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>(); }
1 回答

幕布斯6054654
TA贡献1876条经验 获得超7个赞
我想通了。出于某种原因,我必须使用:
ForType<ConcreteType>.ImportProperty<Interface>(x => x.PublicPropertyToSetInterface);
注册进口时。这使我的简单示例起作用,从而回答了我的问题。我的实际项目仍然存在
System.InvalidCastException: 'Unable to cast object of type 'System.Lazy`1[System.Object]' to type 'Caliburn.Micro.IWindowManager'.'
错误,但这可能是我在注册时遗漏或歪曲的内容。
- 1 回答
- 0 关注
- 115 浏览
添加回答
举报
0/150
提交
取消