【发布时间】:2016-03-01 10:56:55
【问题描述】:
我的引导程序中有以下代码:
private SimpleContainer container;
protected override void Configure()
{
container = new SimpleContainer();
container.Singleton<IEventAggregator, EventAggregator>();
container.PerRequest<InitialViewModel>();
}
protected override object GetInstance(Type service, string key)
{
return container.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
container.BuildUp(instance);
}
在 OnStartup 方法中,我调用了DisplayRooViewFor 方法:
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<InitialViewModel>();
}
这是 InitialViewModel:
private IEventAggregator eventAggregator;
public InitialViewModel(IEventAggregator ea)
{
eventAggregator = ea;
}
不幸的是,它抛出了一个NullReferenceException:
“System.NullReferenceException”类型的异常发生在 Caliburn.Micro.Platform.dll 但未在用户代码中处理
我查看了CM的源代码,并使用相同的代码进行了测试:
protected override void OnStartup(object sender, StartupEventArgs e)
{
var viewModel = IoC.GetInstance(typeof(InitialViewModel), null);
var view = ViewLocator.LocateForModel(viewModel, null, null);
ViewModelBinder.Bind(viewModel, view, null);
var activator = viewModel as IActivate;
if (activator != null)
activator.Activate();
DisplayRootViewFor<InitialViewModel>();
}
奇怪的是,这些行没有异常。 view 和 viewmodel 都有引用,InitialView 的构造函数被调用,但是当它到达并调用DisplayRootViewFor 时,它仍然抛出一个例外。
我应该改变什么?
【问题讨论】:
标签: c# caliburn.micro