Caliburn.Micro DisplayRootViewFor 抛出 NullReferenceException
Caliburn.Micro DisplayRootViewFor throws NullReferenceException
我的引导程序中有以下代码:
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:
An exception of type 'System.NullReferenceException' occurred in
Caliburn.Micro.Platform.dll but was not handled in user code
我查了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
,它仍然抛出异常。
我应该改变什么?
您正在混合使用 SimpleContainer 和 MEF 注入。您应该只使用其中之一。
MEF:
如果您的 InitialViewModel 应该使用 MEF 进行构造函数注入,则您必须创建一个 Bootstrapper 来处理它,就像 post 中那样。请记住导出您的 InitialViewModel 并删除 SimpleContainer 代码。
简单容器
或者您删除 MEF(通过简单地删除 ImportingConstructor-Attribute),SimpleContainer 将接管工作。
你的 InitialViewModel 应该继承 Caliburn.Micro Screen class 如果它附加到 main window。
初始化();方法需要在您的 Bootstrapper CTOR 中调用。
我的容器缺少一个关键组件:
container.Singleton<IWindowManager, WindowManager>();
我的引导程序中有以下代码:
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:
An exception of type 'System.NullReferenceException' occurred in Caliburn.Micro.Platform.dll but was not handled in user code
我查了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
,它仍然抛出异常。
我应该改变什么?
您正在混合使用 SimpleContainer 和 MEF 注入。您应该只使用其中之一。
MEF: 如果您的 InitialViewModel 应该使用 MEF 进行构造函数注入,则您必须创建一个 Bootstrapper 来处理它,就像 post 中那样。请记住导出您的 InitialViewModel 并删除 SimpleContainer 代码。
简单容器 或者您删除 MEF(通过简单地删除 ImportingConstructor-Attribute),SimpleContainer 将接管工作。
你的 InitialViewModel 应该继承 Caliburn.Micro Screen class 如果它附加到 main window。
初始化();方法需要在您的 Bootstrapper CTOR 中调用。
我的容器缺少一个关键组件:
container.Singleton<IWindowManager, WindowManager>();