Using the MVVM Pattern with .NET MAUI Shell

Learn to correctly use the MVVM pattern with .NET MAUI Shell by registering services with DI and overriding the CreateWindow method.

  1. Register the service.

    1
    
    services.AddSingleton<AppShell, ShellViewModel>();
    
  2. Modify App.xaml.cs.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    public App()
    {
        InitializeComponent();
    
        MauiExceptions.UnhandledException += (sender, args) =>
        {
            Exception? exception = args.ExceptionObject as Exception;
        };
    }
    protected override Window CreateWindow(IActivationState? activationState)
    {
        AppShell? appShell = activationState?.Context.Services.GetService<AppShell>();
        Window window = new(appShell!);
        return window;
    }
    
Built with Hugo
Theme Stack designed by Jimmy