The easiest and best MVVM toolkit also for .NET MAUI.

Guys, this is unbelievable. Microsoft Community toolkit has added a tool that means you never have to worry about which MVVM framework you’re voting for. None can be as good as this one. It can be used not only for .NET MAUI, but also for other technologies (e.g. WPF). But before I sing your praises, let’s look at what this package has to offer us:

Introducing CommunityToolkit.Mvvm

First of all, you don’t need to implement INotifyPropertyChanged in ViewModels. And by that I mean not even its parent classes. Since this component is a code generator, it’s enough to make your classes partial, and the INotifyPropertyChanged implementation is done by it, instead of you. This means that your class will be supplemented with the best implementation so that you can easily use common methods like SetProperty in the setter branch of your properties. Okay, okay, but almost any MVVM framework could do this, and you didn’t even need to use a code generator, just simply derive from a parent class. I’ll do you one better: what if you didn’t have to write the setter and getter branches of the properties at all? What if the toolkit made them itself? If you could implement a property declaration from two lines of code?
Well, that’s what makes this toolkit good: It does all that for you.

And if that’s not enough, you don’t even have to bother creating Commands. The toolkit automatically creates a command for you from your methods.
But let’s see how you can use it.

Use CommunityToolkit.MVVM in .NET MAUI

Let’s see what my ViewModel looked like before I used this toolkit:

namespace Carloo.ViewModels.SignInPage
{
    public partial class SignInPageViewModel : BaseViewModel
    {
        private Command signInCommand;
        public Command SignInCommand
        {
            get { return signInCommand ?? (signInCommand = new Command(() => _ = SignIn(), () => true)); }
        }

        private string userName;
        public string UserName
        {
            get { return userName; }
            set { SetProperty(ref userName, value, nameof(UserName)); }
        }

        private string password;
        public string Password
        {
            get { return password; }
            set { SetProperty(ref password, value, nameof(Password)); }
        }

        public SignInPageViewModel()
        {

        }

        private void SignIn()
        {
            try
            {
                ...
            }
            catch (Exception ex)
            {
                // todo error
            }
        }
    }
}

And now let’s see what this Toolkit can do with source code to make life easier:

namespace Carloo.ViewModels.SignInPage
{
    [INotifyPropertyChanged]
    public partial class SignInPageViewModel
    {
        [ObservableProperty]
        private string userName;

        [ObservableProperty]
        private string password;

        public SignInPageViewModel()
        {

        }

        [ICommand]
        private void SignIn()
        {
            try
            {
                ...
            }
            catch (Exception ex)
            {
                // todo error
            }
        }
    }
}

In other ways, however, Resharper also had the tools to provide similar simplicity for implementing MVVM. But if I remember correctly, it didn’t have all that. Like, say, that you could release PropertyChanged on a completely unrelated property, even with an attribute declaration, without writing code.

        [ObservableProperty]
        [AlsoNotifyFor(nameof(CanSignIn))]
        private string _userName;

        [ObservableProperty]
        [AlsoNotifyFor(nameof(CanSignIn))]
        private string _password;


        public bool CanSignIn { get => !string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password); }

Learn more

I am very excited about using this tool in production environments. If you want to learn more about it, please visit the following pages:
https://devblogs.microsoft.com/ifdef-windows/announcing-net-community-toolkit-v8-0-0-preview-1/
https://docs.microsoft.com/en-gb/windows/communitytoolkit/mvvm/introduction

This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

2 thoughts on “The easiest and best MVVM toolkit also for .NET MAUI.”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.