What is MVVM?
MVVM is short for Model-View-ViewModel.
MVVM, of course, three layers-M-V-VM. With this “three-tier” structure, WPF / UWP developers can toss out a complete program. M-defines the data model, V-the view, VM-the view model. The words of M and V are well understood, but the VM is not a daily word.
Therefore, all kinds of code that do not know where to put them are all put into the VM, and eventually, the VM Infinite expansion, hundreds or thousands of lines are also commonplace!
Writing maintainable front-end code is no easy task. We have implemented the separation of back-end data, template pages, and controllers through koa using the MVC pattern, but for the front end, it is not enough.
MVVM is not an application architecture, it is just a development mode for GUI-like programs. This means that it is only used to solve the development problem of the GUI part of our application and cannot be used to solve other problems. And how can a sustainable program have only a GUI?
MVVM is just a development model suggested by data-driven GUI programs; no matter which of the three layers, it is essentially solving UI problems.
Non-UI issues are not discussed at all in MVVM.
- Don’t put the logic you want to test here.
- Not only Window / Page / UserControl, but also Control / DataTemplate.
- You can consider using the Behavior mechanism provided by DataTrigger, ValueConverter, VisualState, or Blend to handle the UI display method corresponding to ViewModel.
- The state of the abstract UI needs to be maintained here so that when multiple Views are created based on this ViewModel, these Views can be completely consistent without having to run aside from the previous logic.
- Views cannot be referenced anyway, not even interfaces.
- Be careful not to call some singleton classes or static classes with the state, so that you can unit test.
- Data collected through various channels
- Cannot reference View or ViewModel
View notification ViewModel
- Recommended for data binding
- Try not to call ViewModel directly, but you can also call it if necessary
ViewModel notification View
- Attribute binding
- Event notification
- Messages (such as the EventAggregator / Message / RX framework)
- Called through an intermediate service
- Pass a delegate directly from View, and ViewModel calls that delegate