dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

System.MissingMethodException: 'No parameterless constructor defined for type' calling MVVM from Shell in C# #6180

Closed devonuto closed 2 years ago

devonuto commented 2 years ago

Description

When following the method shown in https://github.com/brminnick/HackerNews, when calling the page from Shell, results in a "System.MissingMethodException: 'No parameterless constructor defined for type DashboardPage" error under Android or just an unhandled Win32 error on Windows.

Steps to Reproduce

  1. Follow Hackernews example for creating .NET Maui app, page & viewmodel etc
  2. Create AppShell : Shell page
  3. Set MainPage = AppShell
  4. Add new FlyoutItem to Shell:

public AppShell() { Items.Add( new FlyoutItem { Title = "News", Route = nameof(NewsPage), Items.Add( new Tab { Title = "News", Items = { new ShellContent { Title = "News", Route = nameof(NewsPage), ContentTemplate = new DataTemplate(typeof(NewsPage)), } } } ) } ); }

  1. Build and launch in Android Emulator, get above error.
  2. Launch in windows, get Win32 error mauicheck.txt .

Version with bug

Preview 14

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

net6.0-android 21.0, Windows 10.0.17763.0

Did you find any workaround?

Not use this MVVM method

Relevant log output

No response

VladislavAntonyuk commented 2 years ago

You have to register your pages in DI. builder.Services.AddSingleton<DashboardPage>();

devonuto commented 2 years ago

It is registered in the shell itself.
Routing.RegisterRoute(nameof(DashboardPage), typeof(DashboardPage));

Didn't think it needed to be in the DI as well, as it works without MVVM. It's complaining that there is no parameterless path, because it's not parameterless in the MVVM model.

I had abandoned this method, and is now affected by https://github.com/xamarin/XamarinCommunityToolkit/issues/1842 after the latest VS updates.

VladislavAntonyuk commented 2 years ago

That is not enough to register it in shell route. See example from HackerNews: https://github.com/brminnick/HackerNews/blob/main/src/HackerNews/MauiProgram.cs#35

VladislavAntonyuk commented 2 years ago

By the way, what features from Xamarin Community Toolkit do you use? Can you migrate to CommunityToolkit.Maui?

devonuto commented 2 years ago

I am using the Maui toolkit, but I'm getting that same bug, so I assume it's the same issue.

"CommunityToolkit.Maui" Version="1.0.0-pre9" "CommunityToolkit.Maui.Markup" Version="1.0.0-pre9" "CommunityToolkit.Mvvm" Version="8.0.0-preview3"

devonuto commented 2 years ago

Hmm. I noticed I still had my old Xamarin app still sitting unused in the sln , but it wasn't part of the build. I removed it from the sln and it's not happening anymore. Will try the above again.