Caliburn.Micro DisplayRootViewFor抛出NullReferenceException [英] Caliburn.Micro DisplayRootViewFor throws NullReferenceException

查看:105
本文介绍了Caliburn.Micro DisplayRootViewFor抛出NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的引导程序中包含以下代码:

I have the following code in my 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);
}

在OnStartup方法中,我调用 DisplayRooViewFor 方法:

In the OnStartup method, I call the DisplayRooViewFor method:

protected override void OnStartup(object sender, StartupEventArgs e)
{ 
  DisplayRootViewFor<InitialViewModel>();
}

这是InitialViewModel:

This is the InitialViewModel:

    private IEventAggregator eventAggregator;    

    public InitialViewModel(IEventAggregator ea) 
    {
      eventAggregator = ea;
    }

不幸的是,它引发了 NullReferenceException

Unfortunately, it throws a NullReferenceException:



Caliburn.Micro.Platform.dll中发生类型'System.NullReferenceException'的异常,但未在用户代码中处理

An exception of type 'System.NullReferenceException' occurred in Caliburn.Micro.Platform.dll but was not handled in user code

我检查了CM的源代码,并使用相同的代码对其进行了测试:

I checked the source code of CM and used the same code to test it:

  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 ,它仍然会引发异常。

Strangely, there was no Exception at those lines. Both view and viewmodel have reference, and the constructor of InitialView gets called, but when it reaches and calls DisplayRootViewFor, it still throws an exception.

我应该更改什么?

推荐答案

我的容器缺少关键组件:

My container was missing a critical component:

container.Singleton<IWindowManager, WindowManager>();

这篇关于Caliburn.Micro DisplayRootViewFor抛出NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆