[英]Caliburn.Micro WPF: IoC.Get Returns Null
我的代碼如下所示:
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);
}
}
我的ShellViewModel
看起來像這樣:
ShellViewModel.cs
public class ShellViewModel : Conductor<Screen>
{
public ShellViewModel
{
var aViewModel = IoC.Get<AViewModel>();
ActivateItem(aViewModel);
}
}
但是,每當我運行該程序時,都會顯示一個空白屏幕。 當我調試它時,它說aViewModel
為null
。
Bootstrapper
有什么問題嗎?
根據提供的代碼, AViewModel
未在Bootstrapper中的容器中注冊,因此IoC
不知道它的存在,因此當請求Get
該類型時它將返回null。
例如
_container.RegisterPerRequest(typeof(AViewModel), null, typeof(AViewModel));
IoC
需要解析的所有類型都應首先在支持容器中注冊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.