Caliburn.Micro WPF:IoC.Get Returns 空

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);
    }
}

但是每当我 运行 程序时,都会显示一个空白屏幕。当我调试它时,它说 aViewModelnull

Bootstrapper有什么问题吗?

根据提供的代码,AViewModel 未在 Bootstrapper 中的容器中注册,因此 IoC 不知道它的存在,因此当请求 return 时它将为 null Get那个类型

例如

_container.RegisterPerRequest(typeof(AViewModel), null, typeof(AViewModel));

所有需要由 IoC 解析的类型都应首先向后备容器注册。