如何在Windows窗体应用程序中使用Ninject? [英] How to use Ninject in a Windows Forms application?

查看:224
本文介绍了如何在Windows窗体应用程序中使用Ninject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个主窗体的WinForms应用程序:

I have an WinForms application with this Main Form :

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

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

但我在努力注入 ICountRepository 到MainForm的。我该怎么办呢?

but i am struggling to inject ICountRepository into the mainform. How do I do that ?

推荐答案

那么第一个步骤是,从切换:

Well the first steps are to switch from:

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

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

也许澄清编辑或两个关于你要找什么样的事情来实现可能让你更详细的回答。

Perhaps a clarifying edit or two about what sort of thing you're looking to achieve might get you a more detailed answer.

强烈推荐来加快速度与周围的图案,这是@马克塞曼的依赖注入.NET 书(在它的说法,上述品牌的转变您的成分根 - (单个)获取组成您的应用程序的对象图。

Highly recommended to get up to speed with the patterns around this is @Mark Seemann's Dependency Injection in .NET book (in it's parlance, the transformation above makes Main your Composition Root - the (single) Get Composes the object graph of your app.

这篇关于如何在Windows窗体应用程序中使用Ninject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆