Adding a query to GoToAsync URI Navigation in Xamarin Forms Shell causes it to stop working

Viewed 3336

So for this issue I have a pretty simple stack. Main Menu Screen > List Page > Detail Page. For each page I am getting to it using

Shell.Current.GoToAsync({name of page});

so basically a push and then going back in the stack with

Shell.Current.GoToAsync("../");

This is all working fine until I introduced queries to pass data.

So this works fine

Shell.Current.GoToAsync($"{nameof(Page)}");

But this

Shell.Current.GoToAsync($"{nameof(Page)}?Id={some id here}");

Throws this exception

Relative routing to shell elements is currently not supported. 
Try prefixing your uri with ///: ///PageName?Id=3AC71D0B-D8E3-6C18-FFE3-6D41E154F000

Which makes no sense because the navigation clearly works without the query included. Where am I going wrong? Is this bug or is it expected behavior?

2 Answers

Relative routing to shell elements is currently not supported. Try prefixing your uri with ///: ///PageName?Id=3AC71D0B-D8E3-6C18-FFE3-6D41E154F000

That means you are using Relative routes , however this not supports for passing data before Xamarin Forms 4.7.

If using the version of Xamarin Forms before 4.7 .you need to use Absolute routes to pass data , example as follow :

Shell.Current.GoToAsync($"//animals/elephants/elephantdetails?name={elephantName}");

And to receive data, the class that represents the page being navigated to, or the class for the page's BindingContext, must be decorated with a QueryPropertyAttribute for each query parameter:

[QueryProperty("Name", "name")]
public partial class ElephantDetailPage : ContentPage
{
    public string Name
    {
        set
        {
            BindingContext = ElephantData.Elephants.FirstOrDefault(m => m.Name == Uri.UnescapeDataString(value));
        }
    }
    ...
}

More info can refer here : https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#pass-data

=============================Update===============================

As Shane's said , from the version of Xamarin Forms above 4.7, Relative routes also supports passing paramaters now .

I had a similar issue and solved it by registering the page into the AppShell.xaml.cs

Routing.RegisterRoute(nameof(MyPage),
                typeof(MyPage));
Related