我的代碼如下所示:引導程序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
都應該首先向后備容器注冊。
- 1 回答
- 0 關注
- 366 瀏覽
添加回答
舉報
0/150
提交
取消