4

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?

Sev
  • 883
  • 1
  • 14
  • 34

2 Answers2

2

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://learn.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 .

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • Perfect. Thank you for this information, it was exactly what I needed. I have put in a pull request to add this information to the documentation. https://github.com/MicrosoftDocs/xamarin-docs/pull/2952 – Sev Jun 26 '20 at 14:51
  • I have also raised an enhancement request for a more descriptive exception when this happens. https://github.com/xamarin/Xamarin.Forms/issues/11220 – Sev Jun 26 '20 at 15:40
  • @Sev Thanks for putting in a pull request ,and I will also check this issue . If there are good news , github will mark you :-) – Junior Jiang Jun 29 '20 at 01:41
  • @Sev this is not intended behavior. Passing data with relative routes should work fine. I think this might be a bug that was fixed in the latest 4.7 release. I've attached notes here https://github.com/xamarin/Xamarin.Forms/issues/11220 – Shane Neuville Jul 16 '20 at 16:46
  • @ShaneNeuville Thanks for reminder , answer has been updated. :-) – Junior Jiang Jul 27 '20 at 01:46
  • Sorry to resuscitate this, but I'm using 5.0 and seem to have the same problem. Is this a regression or a different problem? – Nathan Phillips Feb 11 '22 at 23:16
2

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

Routing.RegisterRoute(nameof(MyPage),
                typeof(MyPage));
Ruslan
  • 162
  • 2
  • 13