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

Xamarin Forms 5中Shell.Current.GoToAsync(route)未跳转目标页面反而回退的问题求助

Xamarin Forms 5中Shell.Current.GoToAsync(route)未跳转目标页面反而回退的问题求助

大家好,我现在遇到一个非常棘手的问题,希望能得到各位的帮助!

我正在开发一个基于Xamarin Forms 5 + Shell的应用,目前已经完成了90%的功能,之前一直运行得很正常。但最近在某个特定视图里调用Shell.Current.GoToAsync(route)时,没有跳转到指定的路由页面,反而触发了**回退(Pop)**操作,这完全不符合预期。

我可以100%确认所有路由都注册正确,调用时传入的路由也是准确的,实在搞不懂为什么会出现这种情况。有没有哪位朋友遇到过类似的问题?我现在完全卡壳了。

导航服务代码

下面是我使用的导航服务相关函数:

public async Task PopAsync()
{
    await Shell.Current.Navigation.PopAsync();
}

public async Task GoBackAsync()
{
    await Shell.Current.GoToAsync("..");
}

public async Task ReplaceCurrent<TViewModel>(string parameters = null) where TViewModel : BaseViewModel
{
    await GoToAsync<TViewModel>(@"../", parameters);
}

public async Task PushAsync<TViewModel>(string parameters = null) where TViewModel : BaseViewModel
{
    await GoToAsync<TViewModel>("", parameters);
}

private async Task GoToAsync<TViewModel>(string routePrefix, string parameters) where TViewModel : BaseViewModel
{
    var route = routePrefix + typeof(TViewModel).Name;
    if (!string.IsNullOrWhiteSpace(parameters))
    {
        route += $"?{parameters}";
    }
    await Shell.Current.GoToAsync(route);
}

调用PushAsync<DeviceInspectionEditViewModel>()时生成的路由值是"DeviceInspectionEditViewModel"。

路由注册代码

路由注册是这样做的:

Routing.RegisterRoute(nameof(WorkOrderViewModel), typeof(WorkOrderView));
Routing.RegisterRoute(nameof(InspectionListViewModel), typeof(InspectionListView));
Routing.RegisterRoute(nameof(DeviceInspectionViewModel), typeof(DeviceInspectionView));
Routing.RegisterRoute(nameof(DeviceInspectionEditViewModel), typeof(DeviceInspectionEditView));

当前导航栈状态

调用GoToAsync时,当前的导航栈结构如下:

[0] - {Page} null
[1] - WorkOrderView
[2] - InspectionListView
[3] - DeviceInspectionView

触发导航的代码

触发这个异常导航的是下面的方法:

private async Task Edit(CertDevice dev)
{
    var sw = Logger.LogEntry();
    await _navigationService.PushAsync<DeviceInspectionEditViewModel>();
    Logger.LogExit(sw);
}

重要提示:我在调试日志里还看到了"The user pressed the hardware back button"的日志信息,这感觉和当前的导航问题有关联,但我没搞明白为什么会触发这个日志。

备注:内容来源于stack exchange,提问作者mgerety

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 12:49:29