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

.NET 9+Community Toolkit 12关闭Popup时路由匹配歧义异常

解决.NET 9 + Community Toolkit 12路由匹配歧义异常

1. 排查导航栈重复页面实例

异常路由//IMPL_MainPage/IMPL_MainPage/MainPage/SelectionPage/QuizPage存在重复的IMPL_MainPage,说明导航栈里有重复页面实例。先检查页面跳转逻辑,避免多次调用导航方法进入同一页面:

// 导航前检查目标页面是否已在栈中
if (!Shell.Current.Navigation.NavigationStack.Any(p => p is IMPL_MainPage))
{
    await Shell.Current.GoToAsync(nameof(IMPL_MainPage));
}

2. 修正Popup关闭逻辑

Community Toolkit 12对Popup的导航处理有调整,关闭Popup时别误触发页面导航操作:

  • 直接通过Popup实例的IsOpen属性关闭:
    // 假设你的Popup实例为_rulesPopup
    _rulesPopup.IsOpen = false;
    
  • 如果用了PopupService,调用官方提供的关闭方法:
    await PopupService.Instance.TryCloseAsync();
    
  • 检查ClosePopup方法,确保没有误调用GoToAsync这类导航接口导致栈内页面重复叠加。

3. 清理重复的路由注册

检查AppShell.xaml或代码中的路由注册,避免重复注册IMPL_MainPage:

<!-- 确保只注册一次 -->
<ShellContent Route="IMPL_MainPage" ContentTemplate="{DataTemplate local:IMPL_MainPage}" />

同时排查代码中是否多次调用Routing.RegisterRoute注册同一页面路由。

4. 重置导航栈(应急方案)

如果以上方法无效,可在关闭Popup前重置导航栈,确保路由结构正确:

// 保留根页面,清空其他栈元素后重新导航
var rootPage = Shell.Current.Navigation.NavigationStack.First();
Shell.Current.Navigation.NavigationStack.Clear();
Shell.Current.Navigation.NavigationStack.Add(rootPage);
await Shell.Current.GoToAsync("//MainPage/SelectionPage/QuizPage");

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:42:02