Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET MAUI error No parameterless constructor defined for this object [closed]

Occurs when binding my view to the view model.

Error description:

System.MissingMethodException: 'No parameterless constructor defined for type 'yournamespace.view.pagename'.'
like image 372
bryanjez Avatar asked Jan 21 '26 05:01

bryanjez


1 Answers

It turns out I just forgot to register the View and the ViewModel into MauiProgram.cs

In XAML Page Code Behind

public partial class TestPage : ContentPage
{
  public TestPage(TestViewModel testViewModel)
   {
      InitializeComponent();
      BindingContext = testViewModel;
   }
}

In MauiProgram.cs

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });
#if DEBUG
        builder.Logging.AddDebug();
#endif
        builder.Services.AddSingleton<TestViewModel>();
        builder.Services.AddSingleton<TestPage>();

        return builder.Build();
    }
}
like image 146
bryanjez Avatar answered Jan 23 '26 21:01

bryanjez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!