2014-08-31 29 views
0

在我的引導程序文件中,我想顯示我的視圖模型ONH836ViewModel的根視圖。此視圖模型從BaseViewModel,其具有五個參數的單個構造導出:如何使用Caliburn Micro的IOC實例化具有構造函數參數的視圖模型?

public BaseViewModel(IExportedDataMonitor monitor, IGasLabWorklistService worklistService, IRawDataConduit rawDataConduit, 
      ISettingService settingService, IDataReportingService dataReportingService) 

下面是從引導程序相關的代碼:

class AppInitializer : BootstrapperBase 
    { 
     SimpleContainer container = new SimpleContainer(); 

     public AppInitializer() 
     { 
      Start(); 
     } 
     protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) 
     { 
      base.OnStartup(sender, e); 
      DisplayRootViewFor<ONH836ViewModel>(); 
     } 

     protected override void Configure() 
     {   
      container.RegisterSingleton(typeof(IExportedDataMonitor), null, typeof(FakeExportedDataMonitor)); 
      container.RegisterSingleton(typeof(IGasLabWorklistService), null, typeof(FakeGasLabWorklistService)); 
      container.RegisterSingleton(typeof(IRawDataConduit), null, typeof(FakeRawDataConduit)); 
      container.RegisterSingleton(typeof(ISettingService), null, typeof(FakeSettingService)); 
      container.RegisterSingleton(typeof(IDataReportingService), null, typeof(FakeDataReportingService)); 

      container.RegisterSingleton(typeof(BaseViewModel), null, typeof(ONH836ViewModel)); 

     } 

的OnStartup方法拋出在DisplayRootView線一個NullReferenceException。我認爲這是因爲我在配置方法中做錯了。有人可以識別並糾正我的錯誤嗎?

回答

0

開始()應該是Initialize()爲1,假設CM的v2.xx。保存自己的一些輸入參考下面的代碼。

container.Singleton<ONH836ViewModel>();

你的界面,我建議

container.Instance<IExportedDataMonitor>(new FakeExportDataMonitor());

,他們可以鏈接...

container.Instance<IGasLabWorkListService>(new FakeGasLabWorklistService()) .Instance<IRawDataConduit>(new FakeRawDataConduit()) .Instance<ISettingService>(new FakeSettingService());

更可能爲NullReference的原因是由於由於Initialize()未被調用而配置永不被調用在構造函數中。

+0

我不知道鏈接能力;感謝您的洞察力。問題不在於Configure未被調用(它是),而是Null引用發生在重寫的GetInstance方法中。我不確定我在做什麼錯,但是當我評論它時,Null Reference就消失了。 – blueshift 2014-09-01 15:10:04

相關問題