I'm lost with Ninject in WPF.
I'm initializing it in App.xaml but the ITest property in MainWindow.xaml (even with the InjectAttribute) is not getting resolved and remains null.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
IKernel kernel = new StandardKernel();
kernel.Bind<ITest, Test>();
base.OnStartup(e);
}
}
I googled a bit and found out that it doesn't work that way. In trying to find a solution, I ended up with creating IMainWindow with nothing else but "void Show();" and adding it to the MainWindow.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
IKernel kernel = new StandardKernel();
kernel.Bind<ITest, Test>();
kernel.Bind<IMainWindow, MySolution.MainWindow>();
kernel.Get<IMainWindow>().Show();
base.OnStartup(e);
}
}
For this, I'm getting a NullReferenceException on the line with .Get
I also tried this:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
IKernel kernel = new StandardKernel();
kernel.Bind<ITest, Test>();
MainWindow = new MySolution.MainWindow(kernel);
//then kernel.Inject(this); in the MainWindow constructor
MainWindow.Show();
base.OnStartup(e);
}
}
Now I'm getting a NullReferenceException at the .Inject line in the MainWindow.
I found another various solutions but they seemed heavyweight and I gave up testing all of them and trying which one works.
Any help please?
NullReferenceException
is? – Vertigo