You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI Shell导航异常:无法返回主页及路由跳转错误求助

MAUI Shell导航问题求助

导航流程

  • 应用加载后显示LandingPage(主页)
  • 用户点击账户图标,路由跳转至Login页面
  • 用户登录后无法返回LandingPage

相关代码

App Shell XAML

<ShellContent
   Title="LandingPage"
   ContentTemplate="{DataTemplate local:LandingPage}"
   Route="LandingPage" />
</Shell>

AppShell.xaml.cs

public AppShell()
{
    InitializeComponent();

    // 该路由已在XAML中定义,无需注册
    // Routing.RegisterRoute(nameof(LandingPage), typeof(LandingPage));

    Routing.RegisterRoute(nameof(StoreItems), typeof(StoreItems));
    Routing.RegisterRoute(nameof(Register), typeof(Register));
    Routing.RegisterRoute(nameof(PasswordRecovery), typeof(PasswordRecovery));
    Routing.RegisterRoute(nameof(NewStoreRequest), typeof(NewStoreRequest));
    Routing.RegisterRoute(nameof(AccountInfo), typeof(AccountInfo));
    Routing.RegisterRoute(nameof(AllPromotions), typeof(AllPromotions));
    Routing.RegisterRoute(nameof(AllStores), typeof(AllStores));
    Routing.RegisterRoute(nameof(ChangePassword), typeof(ChangePassword));
    Routing.RegisterRoute(nameof(Checkout), typeof(Checkout));
    Routing.RegisterRoute(nameof(CreateAddress), typeof(CreateAddress));
    Routing.RegisterRoute(nameof(TrackOrder), typeof(TrackOrder));
    Routing.RegisterRoute(nameof(TrackOrderDetails), typeof(TrackOrderDetails));
    Routing.RegisterRoute(nameof(StoreBasket), typeof(StoreBasket));
    Routing.RegisterRoute(nameof(ProcessingPayment), typeof(ProcessingPayment));
    Routing.RegisterRoute(nameof(PaymentConfirmed), typeof(PaymentConfirmed));
    Routing.RegisterRoute(nameof(Login), typeof(Login));
}

MauiProgram.cs(更新后)

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureSyncfusionCore()
            .UseMauiCommunityToolkit()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("FontAwesome6BrandsRegular400.otf", "BrandsRegular");
                fonts.AddFont("FontAwesome6DuotoneSolid900.otf", "DuoToneSolid");
                fonts.AddFont("FontAwesome6ProLight300.otf", "ProLight");
                fonts.AddFont("FontAwesome6ProRegular.otf", "ProRegular");
                fonts.AddFont("FontAwesome6ProSolid900.otf", "ProSolid");
                fonts.AddFont("FontAwesome6ProThin100.otf", "ProThin");

                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

        // 服务注册
        builder.Services.AddSingleton<INippyDataService, NippyDataService>();

        // 视图注册
        builder.Services.AddTransient<LandingPage>();
        builder.Services.AddTransient<StoreItems>();
        builder.Services.AddSingleton<Login>();
        builder.Services.AddSingleton<Register>();
        builder.Services.AddSingleton<PasswordRecovery>();
        builder.Services.AddTransient<AccountInfo>();
        builder.Services.AddTransient<AllPromotions>();
        builder.Services.AddTransient<AllStores>();
        builder.Services.AddSingleton<ChangePassword>();
        builder.Services.AddTransient<Checkout>();
        builder.Services.AddTransient<CreateAddress>();
        builder.Services.AddSingleton<NewStoreRequest>();
        builder.Services.AddTransient<TrackOrder>();
        builder.Services.AddTransient<TrackOrderDetails>();
        builder.Services.AddTransient<StoreBasket>();
        builder.Services.AddTransient<ProcessingPayment>();
        builder.Services.AddTransient<PaymentConfirmed>();

        // 视图模型注册
        builder.Services.AddTransient<LandingPageVM>();
        builder.Services.AddTransient<StoreItemsVM>();
        builder.Services.AddSingleton<LoginVM>();
        builder.Services.AddSingleton<RegisterVM>();
        builder.Services.AddSingleton<PasswordRecoveryVM>();
        builder.Services.AddTransient<AccountInfoVM>();
        builder.Services.AddTransient<AllPromotionsVM>();
        builder.Services.AddTransient<AllStoresVM>();
        builder.Services.AddSingleton<ChangePasswordVM>();
        builder.Services.AddTransient<CheckoutVM>();
        builder.Services.AddTransient<CreateAddressVM>();
        builder.Services.AddSingleton<NewStoreRequestVM>();
        builder.Services.AddTransient<TrackOrderVM>();
        builder.Services.AddTransient<TrackOrderDetailsVM>();
        builder.Services.AddTransient<StoreBasketVM>();
        builder.Services.AddTransient<ProcessingPaymentVM>();
        builder.Services.AddTransient<PaymentConfirmedVM>();

#if DEBUG
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }
}

账户按钮跳转代码(更新后)

[RelayCommand]
async Task BtnAccount()
{

    if (CurrentUser != null)
    {
        await Shell.Current.GoToAsync(nameof(AccountInfo), true);
    }
    else
    {
        bool result = await Application.Current.MainPage.DisplayAlert("Account Required", $"You must be logged in to view Account details, do you wish to login now?", "Yes", "No");
        if (result)
        {
            await Shell.Current.GoToAsync(nameof(Login), true);
        }
    }
}

登录按钮点击代码

[RelayCommand]
async Task Login()
{

    if (IsBusy)
        return;

    try
    {
        IsBusy = true;

        if (ValidateFormData())
        {
            User user = await _dataService.LoginUserAsync(Email, Password);
            if (user != null)
            {
                user.Order.Charity = await _dataService.GetCharityAsync();

                if (user.ContactDetails.Email != null)
                {
                    SharedSettings.SetCurrentUser(user);
                    if (!user.ChangePassword)
                    {
                        try
                        {
                            var p = Shell.Current.Navigation.NavigationStack;
                            await Shell.Current.GoToAsync(nameof(LandingPage), true);
                        }
                        catch (Exception ex) 
                        {
                            var p = ex.Message;
                        }
                      

                    }
                    else
                    {
                        await Shell.Current.GoToAsync(nameof(ChangePassword), true);
                    }
                }
                else
                {
                    await Shell.Current.DisplayAlert("Error!", "Login Failed", "OK");
                }
            }
            else
            {
                await Shell.Current.DisplayAlert("Error!", $"Login Failed, account not found", "OK");
            }

            
        }
        else
        {
            await Shell.Current.DisplayAlert("Error!", ErrorMessage, "OK");
        }

    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
        await Shell.Current.DisplayAlert("Error!", $"Service is not responding, please try again later", "OK");
    }
    finally
    {
        IsBusy = false;
    }
 
   
}

尝试的方案及问题

  • 使用await Shell.Current.GoToAsync("..", true):应用挂起并最终超时
  • 使用await Shell.Current.GoToAsync(nameof(LandingPage), true):提示相对路由错误,建议使用..//等格式
  • 其他异常:从LandingPage跳转至AllStores后,使用await Shell.Current.GoToAsync("..", true)却跳转到CreateAddress页面

更新信息

导航栈第0位为空,第1位是Login页面;改为使用nameof(LandingPage)生成新页面实例后,仍出现挂起超时问题。


内容的提问来源于stack exchange,提问作者Kevin Deery

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:07:33