首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullReferenceException on DisplayRootViewFor<>

NullReferenceException on DisplayRootViewFor<>
EN

Stack Overflow用户
提问于 2017-06-05 15:18:41
回答 1查看 173关注 0票数 1

我创建了一个从BootstrapperBase派生的类,覆盖了OnStartup(),并调用了DisplayRootViewFor<AppViewModel>(),就像文档中的一样。

但是当我启动这个应用程序时,我得到了一个NullReferenceException on DisplayRooViewFor<AppViewModel>()

代码语言:javascript
复制
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);
    }
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-05 15:29:47

尝试重写Configure方法并注册视图模型类型:

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44372190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档