如何在Windows窗体应用程序中使用Ninject?

编程入门 行业动态 更新时间:2024-10-27 16:38:46
本文介绍了如何在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?

本文发布于:2023-11-03 07:54:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   应用程序   如何在   Ninject   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!