how to get dependencies injected in constructors in Windows Forms

喜夏-厌秋 提交于 2019-12-03 02:59:00

Yes, in program.cs you should make windsor resolve Form1. For the sake of being able to view the form in the designer you add an empty constructor and decorate it with the Obsolete attribute.

Using Constructor Injection for Forms (or other Views in other UI frameworks) is often problematic because the Visual Studio designer expects and assumes a default constructor.

In any case, a Form or other visual Control really ought to be a dumb View without behavior. It's purpose is to display whatever data you bind to it. Using the data binding features often helps in constraining you into this passive form of display.

This means that you need some sort of Controller which can instantiate the View (Form) and bind the data source to it.

This is a lot easier to do with a technology such as WPF, but it's also possible with Windows Forms. For inspiration on how to do this with Windows Forms, I suggest that you take a look at the (now retired) Composite Application Block - it's overly complicated, but it should give you some ideas on how to implement something similar yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!