admin管理员组

文章数量:1316009

I was previously able to navigate between pages in my .NET MAUI app without any issues. However, after introducing an OnboardingPage, I started encountering errors related to the shell. No matter what fixes I tried, the problem persisted, often resulting in Shell.Current being null.

I made multiple attempts to resolve this, but each time, something new broke. The most recent and consistent error I am getting is:

System.InvalidOperationException: 'Unable to resolve service for type Microsoft.Maui.Controls.Shell while attempting to activate App.Core.Services.NavigationServices'

Troubleshooting steps I tried:

  1. Checked routing configuration:

    • Verified that OnboardingPage was correctly registered in Routing.RegisterRoute() inside AppShell.xaml.cs.

    • Double-checked for any typos in the route names.

    • Navigation routes for other pages (HomePage, HistoryPage, GoalPage) were already working fine before adding onboarding.

  2. Tried delaying shell initialization:

    • Used Task.Delay(100) inside AppShell’s constructor to make sure the Shell loaded first.

    • Even with the delay, Shell.Current still remained null when navigating to onboarding.

  3. Checked dependency injection (DI) setup:

    • I ensured that AppShell was added as a singleton in MauiProgram.cs.

    • The navigation service (NavigationServices) depends on Shell, and I confirmed that DI was properly configured before adding onboarding.

    • However, the error persists, suggesting that Shell is not getting resolved properly by the DI container.

  4. Forced Shell resolution:

    • Inside CreateWindow(), I tried retrieving AppShell using _serviceProvider.GetRequiredService<AppShell>().

    • However, this still results in a null reference error when attempting to navigate.

Current situation & why I am stuck:

  • Before adding OnboardingPage, everything worked as expected.

  • Now, navigation breaks entirely due to Shell not being available in DI.

  • I am out of ideas on how to properly initialize Shell while ensuring it is available for navigation services.

Any insights on what I might be missing or how to fix this?

public partial class App : Application
{
    private readonly IServiceProvider _serviceProvider;

    public App(IServiceProvider serviceProvider)
    {
        InitializeComponent();
        _serviceProvider = serviceProvider;
    }

    protected override Window CreateWindow(IActivationState? activationState)
    {
        var shell = _serviceProvider.GetService<AppShell>();

        if (shell == null)
        {
            throw new InvalidOperationException("AppShell could not be resolved from service provider.");
        }

        bool hasCompletedOnboarding = PreferencesServices.HasCompletedOnboarding();

        // Navigate to the appropriate startup page inside the Shell
        Task.Run(async () =>
        {
            await Task.Delay(100); // Small delay to ensure Shell initializes
            await shell.GoToAsync(hasCompletedOnboarding ? "//home" : "//onboarding");
        });

        return new Window(shell);
    }
}

public AppShell()
{
    InitializeComponent();

    Routing.RegisterRoute("home", typeof(HomePage));
    Routing.RegisterRoute("onboarding", typeof(OnboardingPage));
    Routing.RegisterRoute("history", typeof(HistoryPage));
    Routing.RegisterRoute("goal", typeof(GoalPage));

    Dispatcher.Dispatch(async () => await InitializeShellAsync());
}

private async Task InitializeShellAsync()
{
    await Task.Delay(100);

    bool hasCompletedOnboarding = PreferencesServices.HasCompletedOnboarding();

    if (!hasCompletedOnboarding)
    {
        Console.WriteLine("[DEBUG] Navigating to onboarding...");
        await Shell.Current.GoToAsync("//onboarding");
    }
}

本文标签: cError of shell after adding onboarding page for first time users in NET maui appStack Overflow