As a developer advocate at JetBrains, I find myself exploring technologies in nooks and crannies of the ecosystem previously out of reach. As a lifelong proponent of the web, mobile development has always been on my list to explore, but I rarely found the time to do so. One of the latest technologies I am dabbling with is Multi-platform App UI, more commonly referred to as MAUI.
In this post, I’ll show you how to use the packages CommunityToolkit.Mvvm and Scrutor to quickly register your XAML views and the corresponding ViewModels for a more convenient approach.
CommunityToolkit.Mvvm and the MVVM pattern
The Model-View-View-Model pattern, also referred to as MVVM, is an approach that separates the logic of your views from the language of the View. In the case of MAUI, that view language is typically XAML. Utilizing the pattern leads to a few positive side effects:
- Your ViewModels are much more straightforward to test, with the bulk of your logic exposed through properties and commands.
- Your Views are simpler, binding to properties and commands rather than directly to members on the specific
ContentPage
. - In MAUI, both
ContentPage
and ViewModels can support dependency injection arguments, allowing for the composition of app functionality.
I’m sure there are other benefits to using the MVVM pattern, but these immediately spring to mind when compared to the alternative of dumping all logic in a ContentPage
directly.
The CommunityToolkit.Mvvm
package includes helpers to make adopting the MVVM pattern more straightforward and performant. CommunityToolkit.Mvvm
uses source generators to generate the tedious parts of building MAUI apps, most notably the implementation of INotifyPropertyChanged
and ICommand
instances.
Let’s start with a simple update of the default MAUI template. Next, we’ll add a new MainPageViewModel
and move most of the app’s logic from the MainPage.xaml.cs
file to our new class.
Note: This sample is from a blog post by Mark Timmings. Check it out here.
Next, we’ll update our MainPage.xaml.cs
code.
We’ll also need to update the XAML to take advantage of our new MainPageViewModel
class.
If you were to run this application now, you’d get the following MissingMethodException
exception.
MAUI doesn’t know how to instantiate any classes mentioned in this sample yet. Let’s fix that.
Using Scrutor to Register Views and ViewModels
In the context of an MVVM-powered MAUI app, there are two essential elements: The View and the ViewModel. These concepts are represented by the types ContentPage
and ObservableObject
.
Scrutor can scan for those types and register the instances as themselves with singleton lifetimes.
When we start the application, we should see our MVVM-powered MAUI application.
The advantage to using Scrutor in this instance is that we can continue to expand our application’s functionality with Views and ViewModels. They should all be part of the services collection and work with dependency injection.
I’ve chosen to register all elements as singelton since, in most cases, a mobile application is limited to a single user, and having types registered as scoped or transient is a waste of resources.
I hope you enjoyed this blog post, and please let me know what kind of MAUI apps you’re building. As this is still a burgeoning community and technology, there’s still a lot to learn.
As always, thanks for reading and sharing my posts.