Home > Software design >  XamarinForms Routing problem with PopModalAsync after PushModalAsync -> Ambiguous routes matched
XamarinForms Routing problem with PopModalAsync after PushModalAsync -> Ambiguous routes matched

Time:06-28

I am looking for a solution to the "Ambiguous routes matched for" problem. I have registered Routes in AppShell.xaml.cs:

Routing.RegisterRoute("dashboard", typeof(DashboardPage));
Routing.RegisterRoute("wholesalers", typeof(WholesalersPage));
Routing.RegisterRoute("newOrder", typeof(NewOrderPage));
Routing.RegisterRoute("products", typeof(ProductsPage));
Routing.RegisterRoute("products/add", typeof(ProductAddPage));

After calling the command in ProductAddViewModel.cs

private async void OpenBarcodeReader()
        {
            scanPage = new ZXingScannerPage();

            scanPage.OnScanResult  = (result) =>
            {
                scanPage.IsScanning = false;

                Device.BeginInvokeOnMainThread(async () =>
                {
                    try
                    {
                        await Navigation.PopModalAsync();
                    }
                    catch (Exception ex)
                    {
                        var theproblem = ex;
                    }
                    Product.EAN = result.Text;
                });
            };
            await Navigation.PushModalAsync(scanPage);
        }
    

I get an error:

Ambiguous routes matched for: 
//D_FAULT_TabBar10/D_FAULT_Tab9/products/D_FAULT_ProductAddPage11 
matches found: 
//D_FAULT_TabBar10/D_FAULT_Tab9/products/D_FAULT_ProductAddPage11,
//D_FAULT_TabBar10/D_FAULT_Tab9/products/D_FAULT_ProductAddPage11
Parameter name: uri

What I don't understand is:

a) Why am I getting this error :D

b) If PushModalAsync is executed correctly (barcode reader ppage appears, result.Text is correctly assigned to Product.EAN), then why can't I execute await Navigation.PopModalAsync(); ?

I've looked in the documentation on MS Docs, but I think my brain has already boiled over :-S

CodePudding user response:

  1. When you set the routes, if the pages are in Shell visual hierarchy, set the routes in xaml.

    <Shell ...>
    <FlyoutItem ...
             Route="animals">
    
     <ShellContent ...
                   Route="monkeys" />
    
    </FlyoutItem>
    
    </Shell>
    

    If the pages are not in Shell visual hierarchy, set it with Routing.RegisterRoute. Only register your routes once using either XAML or C# not both.

  2. When you do the navigation in Shell, you could use the Shell.Current.GoToAsync with absolute routes like the code below.

    await Shell.Current.GoToAsync("//animals/monkeys");
    

    For more details, you could refer to the MS docs: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#absolute-routes

  3. If this issue is still existed, could you share your demo to GitHub, then post the link here. I will help you to do the further troubleshooting.

  • Related