DisplayRootViewFor<>上的NullReferenceException [英] NullReferenceException on DisplayRootViewFor<>

查看:129
本文介绍了DisplayRootViewFor<>上的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a class derived from BootstrapperBase, overwrote OnStartup() and call DisplayRootViewFor<AppViewModel>() just like in the documentation.

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

But when i start the app, i get a NullReferenceException on DisplayRooViewFor<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);
    }
}
}


推荐答案

尝试覆盖 Configure 方法并注册您的视图模型类型:

Try to override the Configure method and register your view model type:

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

这篇关于DisplayRootViewFor&lt;&gt;上的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆