我迷失在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