Implementing auto-generated view models
Following the MVVM pattern often involves writing a lot of boilerplate code for properties with INotifyPropertyChanged
support and commands. This task can be tedious and prone to errors caused by copy-pasting. Fortunately, there’s a solution to avoid writing this code manually: use the code generator available in CommunityToolkit.Mvvm.
Getting ready
Start with the project you got after executing the previous recipe (Decoupling the UI from the view model). This project is available at https://github.com/PacktPublishing/.NET-MAUI-Cookbook/tree/main/Chapter02/c2-DecoupleViewAndViewModel.
The code for this recipe is available at https://github.com/PacktPublishing/.NET-MAUI-Cookbook/tree/main/Chapter02/c2-GeneratedViewModels.
How to do it…
Let’s transform the view model from the previous recipe using the code generators from CommunityToolkit.Mvvm
and see how much code we will save:
- Add the
CommunityToolkit...