Looking for clean WinForms MVC tutorial for C# [cl

2020-01-27 10:54发布

问题:


Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 5 years ago.

How to create a rich user interface Windows application, example Photo Shop.

I am looking for clean MVC tutorial for WinForms with C# somewhere. (Not 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.

Or is the preferred methodology for Windows Applications something else?

回答1:

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).

The work is inspired by Jeremy D Miller's series of articles titled 'Build Your Own CAB'



回答2:

It might be worth looking at the Model-View-ViewModel (if you're interested/happy to use WPF particularly).

It's tweaked specifically to work with WPF and Silverlight, and makes use of databinding in order to glue everything together.

There's a number of resources on the web for finding out more, including John Gossman's introduction, and a good article by Josh Smith, who has other resources on his blog relating to MVVM.



回答3:

For Windows applications, the Windows OS inadvertently becomes part of the Controller.

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).

Of course for any commercial application, you would implement your own model and document structures and separate them from the corresponding WinForm objects.

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.

Rather than the MVC pattern, for Windows applications it helps to understand the Windows messaging architecture and how to handle them.