我的引导程序中有以下代码:
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
method:
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<InitialViewModel>();
}
这是初始视图模型:
private IEventAggregator eventAggregator;
public InitialViewModel(IEventAggregator ea)
{
eventAggregator = ea;
}
不幸的是,它抛出了一个空引用异常:
发生“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 and 视图模型有引用,并且构造函数初始视图被调用,但是当它到达并调用时DisplayRootViewFor
,它仍然抛出异常。
我应该改变什么?