亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Caliburn.Micro WPF:IoC.Get 返回 Null

Caliburn.Micro WPF:IoC.Get 返回 Null

C#
開心每一天1111 2021-07-07 09:01:34
我的代碼如下所示:引導程序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.cspublic class ShellViewModel : Conductor<Screen>{    public ShellViewModel    {        var aViewModel = IoC.Get<AViewModel>();        ActivateItem(aViewModel);    }}但是每當我運行程序時,都會顯示一個空白屏幕。當我調試它時,它說aViewModel是null.有什么問題Bootstrapper嗎?
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

根據提供的代碼,AViewModel未在 Bootstrapper 中的容器中注冊,因此IoC不知道它存在,因此當請求Get該類型時它將返回 null

例如

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

所有需要解析的類型IoC都應該首先向后備容器注冊。


查看完整回答
反對 回復 2021-07-11
  • 1 回答
  • 0 關注
  • 366 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號