[英]Caliburn.Micro WPF: IoC.Get Returns Null
My code looks like this: 我的代码如下所示:
Bootstrapper.cs Bootstrapper.cs
public class Bootstrapper : BootstrapperBase
{
private SimpleContainer _container = new SimpleContainer();
public Bootstrapper()
{
Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
base.OnStartup(sender, e);
DisplayRootViewFor<ShellViewModel>();
}
protected override void Configure()
{
_container.Singleton<IEventAggregator, EventAggregator>();
_container.Singleton<IWindowManager, WindowManager>();
_container.RegisterPerRequest(typeof(ShellViewModel), null, typeof(ShellViewModel));
}
protected override object GetInstance(Type service, string key)
{
return _container.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return _container.GetAllInstances(serviceType);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}
And my ShellViewModel
looks like this: 我的ShellViewModel
看起来像这样:
ShellViewModel.cs ShellViewModel.cs
public class ShellViewModel : Conductor<Screen>
{
public ShellViewModel
{
var aViewModel = IoC.Get<AViewModel>();
ActivateItem(aViewModel);
}
}
But whenever I run the program, a blank screen is shown. 但是,每当我运行该程序时,都会显示一个空白屏幕。 When I debug it, it said that the aViewModel
is null
. 当我调试它时,它说aViewModel
为null
。
Is there anything wrong with the Bootstrapper
? Bootstrapper
有什么问题吗?
Based on the code provided, AViewModel
is not registered with the container in the Bootstrapper so IoC
does not know it exists, thus it will return null when requested to Get
that type 根据提供的代码, AViewModel
未在Bootstrapper中的容器中注册,因此IoC
不知道它的存在,因此当请求Get
该类型时它将返回null。
For example 例如
_container.RegisterPerRequest(typeof(AViewModel), null, typeof(AViewModel));
All types that need to be resolved by IoC
should first be registered with the backing container. IoC
需要解析的所有类型都应首先在支持容器中注册。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.