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

MAUI项目导航异常:多次跳转AccountPage后始终返回MainPage

MAUI 页面导航异常问题排查与修复

问题现象

首次从MainPage跳转至AccountPage,再返回MainPage一切正常;但再次尝试跳转AccountPage时,页面始终显示MainPage。菜单内其他标签页跳转正常,仅AccountPage出现该问题。

用户当前代码

using ReFeelApp.MAUI.ViewModels;

namespace ReFeelApp.MAUI.Pages;

public partial class AccountPage : ContentPage
{
    private AccountViewModel _viewModel;

    public AccountPage()
    {
        InitializeComponent();
        _viewModel = new AccountViewModel();
        BindingContext = _viewModel;
    }

  
    private  void OnArrowClicked(object sender, EventArgs e)
    {
        // Navigate to the Home page (Main page)
        Navigation.PushAsync(new MainPage());
        
    }


    private void OnAccountImageTapped(object sender, EventArgs e)
    {
         if (Shell.Current.FlyoutIsPresented)
         {
             Shell.Current.FlyoutIsPresented = false;
         }
         else
         {
             Shell.Current.FlyoutIsPresented = true;
         }
    }
}

问题根源

你在AccountPage的返回逻辑中使用Navigation.PushAsync(new MainPage()),这会在导航栈中新增一个全新的MainPage实例,而非回到之前已存在的MainPage。多次操作后,导航栈中会堆积多个MainPage实例,当再次尝试跳转到AccountPage时,Shell的导航匹配逻辑会优先处理栈顶页面,导致显示的仍是MainPage。

修复方案

方案1:使用返回栈操作替代新建页面

修改OnArrowClicked方法,直接回到导航栈的上一页:

private void OnArrowClicked(object sender, EventArgs e)
{
    // 回到导航栈中的上一页(即最初的MainPage)
    Navigation.PopAsync();
}

方案2:采用Shell路由导航(推荐)

如果项目基于Shell架构,建议通过注册路由统一管理导航:

  1. 在AppShell.xaml.cs中注册页面路由:
public AppShell()
{
    InitializeComponent();
    Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));
    Routing.RegisterRoute(nameof(AccountPage), typeof(AccountPage));
}
  1. 修改跳转逻辑:
  • 从MainPage跳转到AccountPage:
await Shell.Current.GoToAsync(nameof(AccountPage));
  • 从AccountPage返回MainPage:
private async void OnArrowClicked(object sender, EventArgs e)
{
    await Shell.Current.GoToAsync("..");
}

额外检查点

  • 确保AccountPage在Shell的路由配置中没有重复注册
  • 检查菜单跳转AccountPage的逻辑,确认使用的是正确的Shell路由或导航方式

内容的提问来源于stack exchange,提问作者mihai sorinescu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 00:05:57