【问题标题】:NullReferenceException on DisplayRootViewFor<> [duplicate]DisplayRootViewFor<> 上的 NullReferenceException [重复]
【发布时间】:2017-06-05 15:18:41
【问题描述】:

我创建了一个派生自BootstrapperBase 的类,覆盖OnStartup() 并调用DisplayRootViewFor&lt;AppViewModel&gt;(),就像在文档中一样。

但是当我启动应用程序时,我在DisplayRooViewFor&lt;AppViewModel&gt;() 上得到一个NullReferenceException

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

【问题讨论】:

    标签: c# wpf visual-studio caliburn.micro


    【解决方案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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多