在 WinForms 应用程序中利用预先编写的视图代码
我的工作涉及用 C#.NET 编写相对较小的 WinForms 应用程序。有时,我收到的项目与我已经完成的项目略有不同。我已经能够通过将域逻辑抽象到自己的程序集中来利用业务逻辑代码进行重用,并在新项目中简单地引用它。
然而,我不得不重写很多视图代码(例如表单)。我过去曾尝试对 Form 对象使用继承,但即使它有效,它对设计者来说并不是一个好兆头,并且还需要对项目文件进行一些修改才能以正确的顺序进行编译。是否有一种简单的方法来利用我的视图代码在新项目中重复使用?
My job involves writing relatively small WinForms applications in C#.NET. Sometimes I am given projects which are slight variants on projects that I have already completed. I've been able to leverage business logic code for re-use by abstracting the domain logic into its own assembly, and simply reference it in newer projects.
However, I've had to rewrite a lot of the view code (e.g. Forms). I've tried in the past to use inheritance with Form objects, but even though it worked it did not bode well with the designer, and also required some hacking to the project file in order to compile in the correct order. Is there an easy way to leverage my view code to be re-used in new projects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在尝试在 winforms 项目中构建真正的可重用 UI 代码时也遇到了麻烦。
然后我设法通过使用 UserControls 或多或少地实现了这个目标,但有时很麻烦,因为当它的目标是在布局非常不同的不同项目中重用时,通常需要对大量属性/方法进行子类化。
这就是为什么我使用 Helper 类来设置一些控件属性,以这种方式在参数中传递控件。例如:
此外,对于需要更多工作的最复杂的对象,我使用与某些特定控件关联的控制器类。例如,我使用 MyEditableUltraGridController 来准备原始 Infragistic 的 UltraGrid,使其完全可编辑。
然后,考虑到我的窗体上现有的 UltraGrid1 控件,我会:
我对这种基于类的方法感到满意,因为它允许我以舒适的方式保持控件行为并具有良好的灵活性。
I had troubles too to try to build true reusable pieces of UI code in my winforms projects.
Then I managed to achieve this goal more or less by using UserControls, but it is cumbersome sometimes because it often requires a lot of properties / method to be subclassed when it is aimed to be reused in different projects with a very different layout.
That's why I use Helper classes which set some controls properties, passing the control in parameter this way. For example:
Also, for the most complex objects that requires more work, I am using controller classes associated with some specific controls. For example, I use a MyEditableUltraGridController to prepare a raw Infragistic's UltraGrid to be fully editable.
Then, considering an existing UltraGrid1 control on my form, I will have :
I feel confortable with this class based approach, because it allows me to maintain control behaviour a confortable way with a good flexibility.
我不知道您所说的“简单方法”是什么意思,也不知道您的确切用例场景,但您可以将视图逻辑封装到 可重用的 WinForms 控件 放入单独的程序集中。这些控件可能包含不同的属性,允许针对每个项目对其进行个性化设置。
这正是像 Telerik 和 DevExpress 所做的只是您可以更进一步,甚至合并一些可以重用的业务逻辑。
I don't know what you mean by easy way nor I know your exact use case scenario but you can encapsulate view logic into reusable WinForms controls into a separate assembly. Those controls might contain different properties allowing to personalize them for each project.
That's exactly what companies like Telerik and DevExpress do except that you can get a step further and even incorporate some business logic that could be reused.