我无法让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,用RegistrationBuilder替换了CompositionBatch,并摆脱了建议的类和属性,以支持RegistrationBuilder的Fluid API来配置导入和导出。
当它尝试使用_eventAggregator时,我在ShellViewModel的OnActivate重写中得到了NullReferenceException。从未进口过。
如果我通过注释掉OnActivate()和OnDeactivate()来运行它,它将启动并显示一个空白窗口,因此它将正确地加载外壳程序。它只是不导入任何依赖项。
这是显示问题的最简单的Bootstrapper和ViewModel。
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>();
// These added based on a reference I read, to use ImportProperty
vmBuilder.ForTypesDerivedFrom<IShell>().ImportProperty<IEventAggregator>(x => x.EventAggregator);
vmBuilder.ForTypesDerivedFrom<IShell>().ImportProperty<IWindowManager>(x => x.WindowManager);
AggregateCatalog catalog = new AggregateCatalog();
AssemblyCatalog cmAssembly = new AssemblyCatalog(typeof(IEventAggregator).GetTypeInfo().Assembly, cmBuilder);
AssemblyCatalog cmpAssembly = new AssemblyCatalog(typeof(IWindowManager).GetTypeInfo().Assembly, cmpBuilder);
AssemblyCatalog vmAssembly = new AssemblyCatalog(typeof(MefBootstrapper).GetTypeInfo().Assembly, vmBuilder);
catalog.Catalogs.Add(cmAssembly);
catalog.Catalogs.Add(cmpAssembly);
catalog.Catalogs.Add(vmAssembly);
_container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection | CompositionOptions.IsThreadSafe);
}
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[] {
typeof(IWindowManager).GetTypeInfo().Assembly,
typeof(IEventAggregator).GetTypeInfo().Assembly,
typeof(MefBootstrapper).GetTypeInfo().Assembly
};
}
protected override object GetInstance(Type serviceType, string key)
{
string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
var exports = _container.GetExportedValues<object>(contract);
if (exports.Any())
return exports.First();
throw new Exception(string.Format("Could not locate any instances of contract {0}", contract));
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return _container.GetExports<object>(AttributedModelServices.GetContractName(serviceType));
}
protected override void BuildUp(object instance)
{
_container.SatisfyImportsOnce(instance);
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<IShell>();
}
}
}
using Caliburn.Micro;
namespace MEF2Test.ViewModels
{
public class ShellViewModel : Screen, IShell
{
private IWindowManager _windowManager;
private IEventAggregator _eventAggregator;
public ShellViewModel()
{
}
protected override void OnActivate()
{
base.OnActivate();
_eventAggregator.Subscribe(this);
}
protected override void OnDeactivate(bool close)
{
base.OnDeactivate(close);
_eventAggregator.Unsubscribe(this);
}
// These 2 Properties added based on a reference I read, to use ImportProperty
public IEventAggregator EventAggregator
{
get { return _eventAggregator; }
set { _eventAggregator = value; }
}
public IWindowManager WindowManager
{
get { return _windowManager; }
set { _windowManager = value; }
}
}
public interface IShell
{
// These added based on a reference I read, to use ImportProperty
IEventAggregator EventAggregator { get; set; }
IWindowManager WindowManager { get; set; }
}
}
答案 0 :(得分:0)
我知道了。由于某些原因,我必须使用:
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'.'
错误,但这可能是我在注册中遗漏或歪曲的东西。