我迷失在Ninject在WPF中。WPF应用程序与Ninject
我在App.xaml中初始化它,但MainWindow.xaml中的ITest属性(即使使用InjectAttribute)没有得到解决,仍然为空。
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
IKernel kernel = new StandardKernel();
kernel.Bind<ITest, Test>();
base.OnStartup(e);
}
}
我google了一下,发现它不能这样工作。在试图找到解决方案时,我最终创建了IMainWindow,但没有其他任何内容,但“void Show();”并将其添加到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);
}
}
对于这一点,我得到一个NullReferenceException上的线与不用彷徨
我也试过这样:
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);
}
}
现在我在.Inject线得到一个NullReferenceException在MainWindow中。
我发现了另一个不同的解决方案,但他们似乎重量级,我放弃了测试所有这些,并尝试哪一个工程。
请帮忙吗?
究竟什么是你的'NullReferenceException'是什么? – AgentFire