简体   繁体   English

使用Ninject和MVVM Light

[英]Using Ninject with MVVM Light

Using MVVM Light in a WPF MVVM application. 在WPF MVVM应用程序中使用MVVM Light。

I want to use Ninject instead of SimpleIOC. 我想使用Ninject而不是SimpleIOC。

Even in a brand new WPF/MVVM Light v4 project, I get a null reference for MainViewModel when the Main Property in the ViewModelLocator is called by the XAML. 即使在全新的WPF / MVVM Light v4项目中,当XAML调用ViewModelLocator中的Main属性时,我也会获得MainViewModel的空引用。

 private static readonly StandardKernel kernel;

    static ViewModelLocator()
    {
        if (ViewModelBase.IsInDesignModeStatic)
        {
        }
        else
        {
            kernel = new StandardKernel(new mymodule());
        }
    }

    public MainViewModel Main
    {
        get { return kernel.Get<MainViewModel>(); }
    }

MyModule looks like this: MyModule看起来像这样:

    public class mymodule:NinjectModule
{
    public override void Load()
    {
        Bind<MainViewModel>().ToSelf();
    }
}

I've also tried 我也试过了

    public class mymodule:NinjectModule
{
    public override void Load()
    {
        Bind<MainViewModel>().To<MainViewModel();
    }
}

Ninject kernel's .Get<T> does not return null. Ninject内核的.Get<T>不返回null。

Except in case you explicitly tell it to by doing: 除非您通过以下方式明确告诉它:

  • Bind<T>().ToConstant(null);
  • Bind<T>().ToMethod(x => null);
  • Bind<T>().ToProvider<TProvider>() --> and TProvider.Create(...) returns null Bind<T>().ToProvider<TProvider>() - >和TProvider.Create(...)返回null

It's very unlikely you have any of these. 你不可能有这些。 So if there's a NullReferenceException when accessing the Main property, it must be because private static readonly StandardKernel kernel is null. 因此,如果在访问Main属性时存在NullReferenceException ,则必须是因为private static readonly StandardKernel kernel为null。 Now if the code you've provided us is a Minimal, Complete, and Verifiable example , that means ViewModelBase.IsInDesignModeStatic returns true . 现在,如果您提供的代码是Minimal,Complete和Verifiable示例 ,则表示ViewModelBase.IsInDesignModeStatic返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM