0

when I click on the button to go to the page, an exception appears " System.MissingMethodException: "It is not possible to dynamically create an instance of the type 'MuseumFinder.Views.The museum list page for administrators". Reason: The constructor without parameters is not defined."

But I do not know how to fix it so that the view displays the result of the viewmodel and at the same time the transition to the page is carried out.

 private async void OnCounterClicked_1(object sender, EventArgs e)
    {
        await AppShell.Current.GoToAsync(nameof(MuseumListPageForAdmins));
    }
using MuseumFinder.ViewModels;

namespace MuseumFinder.Views;

public partial class MuseumListPageForAdmins : ContentPage
{
   

    private MuseumListPageViewModel _viewModel;
    public MuseumListPageForAdmins(MuseumListPageViewModel viewModel)
    {
        _viewModel = viewModel;
        this.BindingContext = viewModel;

    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        _viewModel.GetMuseumListCommand.Execute(null);

    }

 private readonly IMuseumService _museumService;
        public MuseumListPageViewModel(IMuseumService museumService)
        {
            _museumService = museumService;
        }
Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
John B
  • 13
  • 3
  • 1
    Does this answer your question? [Injecting services into view models in .NET MAUI app](https://stackoverflow.com/questions/72429297/injecting-services-into-view-models-in-net-maui-app). Despite the different title, that question is about the same error, "no parameterless constructor". The accepted answer shows what to do. – ToolmakerSteve May 20 '23 at 19:05

1 Answers1

0

The way your using it now looks like some Dependency Injection style, I haven't used before.

Appshell needs a parameterless constructor to create a new view. Since the routing is not passing any parameters, you don't need to specify the ViewModel in the constructor

Try the following code

using MuseumFinder.ViewModels;

namespace MuseumFinder.Views;

public partial class MuseumListPageForAdmins : ContentPage
{
   

    private MuseumListPageViewModel _viewModel = new();
    public MuseumListPageForAdmins()
    {
        InitializeComponent();
        BindingContext = _viewModel;
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        _viewModel.GetMuseumListCommand.Execute(null);

    }

I've changed so the view makes a new instance of the viewmodel on creation and sets the BindingContexts to it.

EDIT: Remember the InitializeComponent() function to render the controls of the view. Nearly forgot it myself in the answer ;)

TheKlinto
  • 39
  • 6
  • But now I do not know what to do with my view model constructor. I added the code to the question, please take a look. – John B May 20 '23 at 17:01
  • @JohnB take a look at this issue https://stackoverflow.com/questions/72429297/injecting-services-into-view-models-in-net-maui-app seems to have the answer you are looking for – TheKlinto May 20 '23 at 17:05
  • *"Appshell needs a parameterless constructor to create a new view."* If using DI, this is not true. As shown in Gerald's answer you link. – ToolmakerSteve May 20 '23 at 19:04
  • @ToolmakerSteve I'll review and update my answer. Didn't know about dependency injection in MAUI. And educate myself a little on the topic. – TheKlinto May 21 '23 at 08:22