chapter nine

9 The MVVM pattern

 

In this chapter:

  • The Model, View, ViewModel paradigm
  • The problems that MVVM solves in complex apps
  • Making Controls MVVM friendly with Behaviors

The Model, View, ViewModel (MVVM) pattern was introduced by Microsoft with WPF and has become the standard for apps developed using XAML. It’s a popular pattern with enough nuance that entire books have been dedicated to the subject. This chapter will provide an introduction, and as you progress through your .NET MAUI journey, you may find that this is a topic that you want to explore in more depth.

Use of the MVVM pattern is, in some ways, more an art than a science. You should aim to understand he rules about separating UI logic, presentational logic and business logic if you are adopting the MVVM pattern in your app. Of course, blind adherence to any pattern is an anti-pattern, and you should prioritise ensuring that your code is readable and maintainable. But you can’t make an informed decision about when to deviate from the pattern if you don’t understand it well.

9.1 Refactoring the MauiTodo app for MVVM

9.2 Use behaviors to augment your controls

9.3 What is MVVM?

9.3.1 The Model

9.3.2 The View

9.3.3 The ViewModel

9.3.4 Binding from Views to ViewModels

9.3.5 Putting it all together

9.4 The MauiStockTake app without MVVM

9.5 The MauiStockTake app in MVVM

9.5.1 The Model

9.5.2 The ViewModel

9.5.3 The View

9.5.4 Adding the Search functionality

9.5.5 Adding the Inventory Functionality

9.6 Review of the MauiStockTake App So Far

9.7 Summary