When I setup the compiled data binding as per This article, I forgot to register the view model file in the MauiProgram.cs file. So adding the line
builder.Services.AddSingleton<MainViewModel>();
to the MauiProgram.cs file solved the problem.