Reputation: 277
Using MVVM Light in a WPF MVVM application.
I want to use Ninject instead of 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.
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:
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();
}
}
Upvotes: 1
Views: 1303
Reputation: 13233
Ninject kernel's .Get<T>
does not return 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 nullIt'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.
Now if the code you've provided us is a Minimal, Complete, and Verifiable example, that means ViewModelBase.IsInDesignModeStatic
returns true
.
Upvotes: 3