MAUI嵌套页面返回触发Ambiguous routes matched异常的原因与解决
异常原因与修复方案
异常原因
你在AppShell中同时执行了两项重复的路由操作:给ShellContent设置Route="main"的同时,又手动调用Routing.RegisterRoute("main", typeof(MainPage))注册相同路由。Shell会自动为带有Route属性的ShellContent完成路由注册,这就导致路由表中出现了重复的"main"路由条目。当在DetailsPage调用GoToAsync("..")返回时,Shell解析相对路径会匹配到多个完全一致的路由,触发"路由歧义"的参数异常。
修复步骤
移除重复的手动根路由注册即可,保留ShellContent的Route设置(Shell会自动完成根路由的注册),子路由的手动注册不受影响。
修改后的AppShell构造函数代码:
public partial class AppShell : Shell { public AppShell() { Routing.RegisterRoute("main/card", typeof(CardPage)); Routing.RegisterRoute("main/card/details", typeof(DetailsPage)); Items.Add(new ShellContent { ContentTemplate = new DataTemplate(typeof(MainPage)), Title = "Main", Route = "main" }); InitializeComponent(); } }
修改后路由表中不会再有重复条目,调用GoToAsync("..")返回时就能正确匹配到唯一的父级路由,异常即可解决。
内容的提问来源于stack exchange,提问作者Vlad
相关产品推荐
相关产品推荐

