简体   繁体   中英

Use MVC instead of MVVM in Windows Presentation Foundation

I recently started learning the Windows ecosystem (C#, .NET, WPF and XAML) and as expected is a totally different world. One thing that I would like to start doing from the beginning is to create my apps following the MVVM pattern since it seems to be the standard for WPF applications; I have been searching for good tutorials for beginners and so far I haven't found one that show step by step how to implement MVVM.

I have done a little bit of iOS programming so I'm used to the MVC pattern and I was wondering if it would make sense to follow the same design patter for WPF applications.

This is how I was thinking to structure my code following the MVC pattern.

Model - Data Classes
View  - MainWindow.xaml
Controller - MainWindow.xaml.cs 

Do you guys see any problem with the above file structure?

What is the main benefit of using MVVM ? I'm a little confused about data binding which seems to be very tight to the MVVM pattern in WPF .

Thanks

I Would use a MainView.cs as View that calls Methods inside MainWindow.xaml.cs also use an MainController.cs and use MainWindow.xaml.cs to call methods inside the MainController.cs class. When you splitting MainWindow from your View and Controller by using IController and IView (Interfaces) you can more easily port it. So:

MainWindow.xaml is Layout file only.

Interfaces:

  • IView
  • IController

Classes:

  • MainWindow.xaml.cs uses IView
  • MainController.cs uses IController
  • MainView.cs uses IView
  • Model.cs

Usage: If eg a button is clicked MainWindow.xaml.cs calls Methods from MainController. If you want update eg a textview you call MainView which is calling MainWindow.xaml.cs. If you then Port to eg Android you have to make a new Class that uses IView, is called instead of MainWindow.xaml.cs for updating a textview. Also you use this new class to call the Methods inside MainController.

Edit:

IController is just needed in my Special case because by tcp Connection i use another Controller class so you propably only have to use some iview Interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM