2017-06-05 26 views
1

我創建了一個從BootstrapperBase派生的類,覆蓋了OnStartup(),並調用DisplayRootViewFor<AppViewModel>(),就像在文檔中一樣。DisplayRootViewFor上的NullReferenceException <>

但是,當我啓動應用程序,我得到一個NullReferenceExceptionDisplayRooViewFor<AppViewModel>()

using Caliburn.Micro; 
using MHBRestore.Logic; 
using MHBRestore.UI.ViewModel; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 

namespace MHBRestore.UI 
{ 
public class AppBootstrapper : BootstrapperBase 
{ 
    private SimpleContainer _container = new SimpleContainer(); 

    public AppBootstrapper() 
    { 
     Initialize(); 
    } 

    protected override void OnStartup(object sender, StartupEventArgs e) 
    { 
     DisplayRootViewFor<AppViewModel>(); 
    } 

    protected override object GetInstance(Type service, string key) 
    { 
     return _container.GetInstance(service, key); 
    } 

    protected override IEnumerable<object> GetAllInstances(Type service) 
    { 
     return _container.GetAllInstances(service); 
    } 

    protected override void BuildUp(object instance) 
    { 
     _container.BuildUp(instance); 
    } 
} 
} 

回答

1

嘗試重寫Configure方法和註冊您的視圖模型類型:

public class AppBootstrapper : BootstrapperBase 
{ 
    private SimpleContainer _container = new SimpleContainer(); 

    public AppBootstrapper() 
    { 
     Initialize(); 
    } 

    protected override void Configure() 
    { 
     _container.Singleton<IWindowManager, WindowManager>(); 
     _container.Singleton<IEventAggregator, EventAggregator>(); 
     _container.RegisterPerRequest(typeof(AppViewModel), null, typeof(AppViewModel)); 
    } 

    protected override void OnStartup(object sender, StartupEventArgs e) 
    { 
     DisplayRootViewFor<AppViewModel>(); 
    } 

    protected override object GetInstance(Type service, string key) 
    { 
     return _container.GetInstance(service, key); 
    } 

    protected override IEnumerable<object> GetAllInstances(Type service) 
    { 
     return _container.GetAllInstances(service); 
    } 

    protected override void BuildUp(object instance) 
    { 
     _container.BuildUp(instance); 
    } 
} 
相關問題