Estoy jugando con .Net Maui, AppShell y la inyección de dependencia.
Intento llamar a una página con un constructor que toma el ViewModel de esta página como parámetro.
El constructor se ve así:
public AuthenticationPage(AuthenticationViewModel viewModel) { InitializeComponent(); BindingContext = viewModel; }En mi MauiProgram.cs registré tanto la página como la VM
builder.Services.AddSingleton<AuthenticationViewModel>(); builder.Services.AddSingleton<AuthenticationPage>();Mi App.xaml.cs se ve así:
public partial class App : Application { public App() { InitializeComponent(); MainPage = new AppShell(); } }Y mi AppShell.xaml se ve así:
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:pages="clr-namespace:DeepBlue.Pages" xmlns:auth="clr-namespace:DeepBlue.Pages.Authentication" x:Class="DeepBlue.AppShell"> <!-- Login and Registration Page --> <ShellContent Route="login" ContentTemplate="{DataTemplate auth:AuthenticationPage}"> </ShellContent> <!-- Main Page --> <FlyoutItem Route="main" FlyoutDisplayOptions="AsMultipleItems"> <ShellContent Route="dashboard" ContentTemplate="{DataTemplate pages:DashboardPage}" Title="Home" /> </FlyoutItem> </Shell>Ahora, cuando ejecuto mi proyecto, aparece el siguiente error:
System.MissingMethodException: 'No se definió un constructor sin parámetros para el tipo 'DeepBlue.Pages.Authentication.AuthenticationPage'.'
¿Podría alguien decirme por qué la inyección de dependencia no funciona en este caso?
Si no implemento AppShell, todo funciona bien... Puedo llamar a AuthenticationPage como MainPage a través de una inyección desde mi App.xaml.cs así:
public App(AuthenticationPage page) { InitializeComponent(); MainPage = page }gracias, phil
Shell (y DataTemplates que está asociado) no tiene soporte para inyección de dependencia (todavía). Los problemas en el repositorio se abren aquí y aquí . Y al momento de escribir esta respuesta está abierto un PR que agrega esta funcionalidad. Puede seguir el progreso de eso aquí .