2010-11-08 110 views
16

我有這個主窗體的WinForms應用程序:如何在Windows窗體應用程序中使用Ninject?

ICountRepository countRepository; 
    public MainForm(ICountRepository countRepository) 
    { 
     this.countRepository = countRepository; 
    } 

    public void IncrementCount() 
    { 
     countRepository.IncrementCount(); 
    } 

,但我在努力注入ICountRepository到MainForm的。我怎麼做 ?

+0

相關:http://stackoverflow.com/questions/19937187/does-di-make-sense-in-a-desktop-app – 2013-11-14 15:38:43

回答

20

嘛第一步是從切換:

var form = new MainForm(); 
Application.Run(form); 

到:

var kernel = new StandardKernel(new ModuleRegisteringICountRepository()); 
var form = kernel.Get<MainForm>(); 
Application.Run(form); 

也許你正在尋找實現可能讓你一個什麼樣的事情澄清編輯或兩個更詳細的答案。


強烈推薦起牀與模式,以加快解決此是@馬克西曼的Dependency Injection in .NET書(在它的說法,改造上面,使MainComposition Root - 您的應用程序的(單)GetComposes the object graph

+1

我終於這樣做了Program.cs文件中的主要方法 – Attilah 2010-11-09 14:46:42

+0

@Attillah:是的,有道理 – 2010-11-09 17:22:07

相關問題