为 C# 寻找干净的 WinForms MVC 教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/955973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looking for clean WinForms MVC tutorial for C#
提问by s5804
How to create a rich user interface Windows application, example Photo Shop.
如何创建丰富的用户界面 Windows 应用程序,例如 Photo Shop。
I am looking for clean MVC tutorial for WinForms with C# somewhere. (NotASP.NET MVC.)
我正在寻找带有 C# 的 WinForms 的干净 MVC 教程。(不是ASP.NET MVC。)
Being new on the Windows Platform; most MSDN and internet tutorials basically puts everything into the Form class. Further Default events are handled inside of the form, instead of sending events to the control/model, which in it's turn changes the view's state.
作为 Windows 平台的新手;大多数 MSDN 和 Internet 教程基本上都将所有内容都放入 Form 类中。进一步的默认事件在表单内部处理,而不是将事件发送到控件/模型,而后者又会更改视图的状态。
Or is the preferred methodology for Windows Applications something else?
或者 Windows 应用程序的首选方法是什么?
回答by Dave Arkell
It might be worth looking at the Model-View-ViewModel (if you're interested/happy to use WPF particularly).
可能值得查看 Model-View-ViewModel (如果您有兴趣/特别乐意使用 WPF)。
It's tweaked specifically to work with WPF and Silverlight, and makes use of databinding in order to glue everything together.
它专门针对 WPF 和 Silverlight 进行了调整,并利用数据绑定将所有内容粘合在一起。
There's a number of resources on the web for finding out more, including John Gossman's introduction, and a good articleby Josh Smith, who has other resources on his blogrelating to MVVM.
网络上有许多资源可用于了解更多信息,包括 John Gossman 的介绍和Josh Smith 的一篇好文章,他的博客上有与 MVVM 相关的其他资源。
回答by Sesh
For Windows applications, the Windows OS inadvertently becomes part of the Controller.
对于 Windows 应用程序,Windows 操作系统无意中成为控制器的一部分。
The Model can subscribe to various events/ messages (e.g. mouse click, move, window re-size, button click...). When it has done its change-update-processing the view is updated through the redraw operations.
模型可以订阅各种事件/消息(例如鼠标单击、移动、窗口大小调整、按钮单击...)。当它完成更改更新处理后,视图将通过重绘操作更新。
Windows OS acts as the controller here because it is both sending messages to the model (e.g. mouse click) or to the view (telling it a portion of windows is dirty and needs redraw).
Windows 操作系统在这里充当控制器,因为它既向模型发送消息(例如鼠标点击),也向视图发送消息(告诉它一部分窗口是脏的,需要重绘)。
Of course for any commercial application, you would implement your own model and document structures and separate them from the corresponding WinForm objects.
当然,对于任何商业应用程序,您将实现自己的模型和文档结构,并将它们与相应的 WinForm 对象分开。
In .net this is not so clear because there is only one file for both the model and view portions. In MFC it was very clear - with a CDocument class and its corresponding CView class.
在 .net 中,这不是很清楚,因为模型和视图部分只有一个文件。在 MFC 中,它非常清楚——有一个 CDocument 类和它对应的 CView 类。
Rather than the MVC pattern, for Windows applications it helps to understand the Windows messaging architecture and how to handle them.
对于 Windows 应用程序,它有助于理解 Windows 消息传递体系结构以及如何处理它们,而不是 MVC 模式。
回答by rohancragg
Derick Bailey (via Los Techies) has blogged some fantastic articles(and complete demo source code) for his implementation of MVP in a WinForms app with a couple of other good patterns added to the mix (Application Controller, Event Aggregator).
Derick Bailey(通过 Los Techies)在博客上写了一些很棒的文章(和完整的演示源代码),用于在 WinForms 应用程序中实现 MVP,并添加了一些其他好的模式(应用程序控制器、事件聚合器)。
The work is inspired by Jeremy D Miller's series of articles titled 'Build Your Own CAB'