我的Bootstrapper中有以下代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
在OnStartup方法中,我调用该DisplayRooViewFor方法:
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<InitialViewModel>();
}
Run Code Online (Sandbox Code Playgroud)
这是InitialViewModel:
private IEventAggregator eventAggregator;
public InitialViewModel(IEventAggregator ea)
{
eventAggregator = ea;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它抛出了NullReferenceException:
Caliburn.Micro.Platform.dll中出现"System.NullReferenceException"类型的异常,但未在用户代码中处理
我检查了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>();
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,这些方面没有例外.这两种看法和视图模型具有一定的参考,并构造InitialView被调用,但是当它到达并调用DisplayRootViewFor,它仍然会抛出异常.
我应该改变什么?
Nes*_*tor 10
我的容器缺少一个关键组件:
container.Singleton<IWindowManager, WindowManager>();
Run Code Online (Sandbox Code Playgroud)