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:
Checked routing configuration:
Verified that
OnboardingPage
was correctly registered inRouting.RegisterRoute()
insideAppShell.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.
Tried delaying shell initialization:
Used
Task.Delay(100)
insideAppShell
’s constructor to make sure the Shell loaded first.Even with the delay,
Shell.Current
still remainednull
when navigating to onboarding.
Checked dependency injection (DI) setup:
I ensured that
AppShell
was added as a singleton inMauiProgram.cs
.The navigation service (
NavigationServices
) depends onShell
, 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.
Forced
Shell
resolution:Inside
CreateWindow()
, I tried retrievingAppShell
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
版权声明:本文标题:c# - Error of shell after adding onboarding page for first time users in .NET maui app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741994837a2409833.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论