AppShell- Failed to Navigate Back, Ambiguous routes matched for.... but i've only registered routes once

Nabil 6 Reputation points
2023-03-26T23:28:07.2466667+00:00

I am building an Xamarin Forms app for iOS and Android. I registered my routes in AppShell.xaml.cs globally like this:

**-AppShell.xaml.cs-**

Routes.Add(nameof(HousingFolderPage), typeof(HousingFolderPage));

Routes.Add(nameof(HousingFolderDetailsPage), typeof(HousingFolderDetailsPage));

In my tabbar I have a tab "MoreServicePage" which shows a list of items. So far so good.

**-MoreServicePage.cs-**

private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)

{
    await Shell.Current.GoToAsync($"HousingFolderPage");
}

Now when HousingFolderPage is showing, I have no problem navigating back from here. HousingFolderPage also shows another list, but when I push to next page, I'm not able to come back.

**-HousingFolderPage.xaml.cs-**

private async void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{

    await Shell.Current.GoToAsync("HousingFolderDetailsPage");

}

Error message:

[0:] Shell: Failed to Navigate Back: System.ArgumentException: Ambiguous routes matched for: //D_FAULT_TabBar2/IMPL_MoreServicePage/MoreServicePage/HousingFolderPage matches found: //D_FAULT_TabBar2/IMPL_MoreServicePage/MoreServicePage/HousingFolderPage,//D_FAULT_TabBar2/IMPL_MoreServicePage/MoreServicePage/HousingFolderPage Parameter name: uri

I have only registered the routs in AppShell programmaticly (AppShell.xaml.cs) and no other places not even in the AppShell.xaml.

PS. this seems to happen everywhere where I navigate 2 pages deep, while 1 page deep works without a problem

I have created a simple Xamarin Forms Application to reproduce this issue. As you will see, you are able to navigate back from page 2, but not from page 3 to page 4 nor from page 4 to page 3.

Download test repo:

https://github.com/nabilakhlaque/Xamarin-forms-navigation-issue

Developer technologies | .NET | Xamarin
Developer technologies | ASP.NET | Other
Developer technologies | C#
{count} vote

3 answers

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-03-27T01:46:55.8433333+00:00

    Hello,

    Ambiguous routes matched for: ....

    The reason for this problem is that you have routes defined in both XAML and C#, in fact, you only need to register the routes in one place.

    On my test, after deleting Route="Page1" in AppShell.xaml, the project worked fine.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    5 people found this answer helpful.

  2. Anonymous
    2024-12-11T14:00:09.7133333+00:00

    I had the same problem, and the issue was in the MauiProgram.cs file. The registration of the page in MauiProgram.cs sometimes needs to use AddSingleton instead of AddTransient. I am still a student, so I plan to research and ask more about the difference between AddTransient and AddSingleton, but my issue was solved by changing the registration from AddTransient to AddSingleton.

    Additionally, it's important to make sure that the same route is not registered twice. I believe registering routes in the AppShell.xaml.cs is enough in most cases, but for some pages, you also need to include them in the AppShell.xaml as ShellContent.

    0 comments No comments

  3. Paul Reedy 0 Reputation points
    2025-02-26T17:38:59.5166667+00:00

    I had this happen to me. In the process of getting the selectedItem changed event to work, I inadvertently declared the event in both the Xaml and the codebehind in the constructor. Once I removed the one in the constructor, it then only fired once.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.