A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Hello,
This issue is caused by incorrect usage of the AddSingleton method.
If you pass in two parameters builder.Services.AddSingleton<MainPage, MainViewModel>(); when using this method, according to the description in the ServiceCollectionServiceExtensions.AddSingleton Method, you need to pass in a Service interface and its implementation class.
AddSingleton<TService,TImplementation>(IServiceCollection): Adds a singleton service of the type specified in
TServicewith an implementation type specified inTImplementationto the specified IServiceCollection.
For example, the official documentation gives the demonstration of mauiAppBuilder.Services.AddTransient<ISettingsService, SettingsService>();. Although this demonstration method is AddTransient, it is still applicable to the AddSingleton method.
Since MainViewModel is not an implementation class of MainPage, a type conversion error occurred when the program was created.
Workaround:
Register these classes individually.
builder.Services.AddSingleton<MainPage>();
builder.Services.AddSingleton<MainViewModel>();
builder.Services.AddSingleton<LoginPage>();
builder.Services.AddSingleton<LoginViewModel>();
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.