xamarin.formsroutesurl-routingflyoutargumentexception

Xamarin.Forms: Disabling FlyoutItem and Changing Default Page


I have a Xamarin.Forms project with some FlyoutItems:

MyShell.xaml:

... ...

<FlyoutItem x:Name="Page1" IsEnabled="False" Title="xxxx xxxx" StyleClass="MenuItemLayoutStyle" >
    <ShellContent Route="Page1">
        <local:Page1 Parameter="-1" />
    </ShellContent>
</FlyoutItem>
<FlyoutItem x:Name="Page2" Title="Alle Reisen" Icon="icon_feed.png">
    <ShellContent Route="Page2" ContentTemplate="{DataTemplate local:Page2}" />
</FlyoutItem>
<FlyoutItem Title="About" Icon="icon_about.png">
    <ShellContent Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
</FlyoutItem>     ...     ...

FlyoutItem Page1 should only be enabled when some condition is true, so in codebehind I set IsEnabled depending on my condition:

MyShell.xaml.cs:

public partial class MyShell : Xamarin.Forms.Shell
{
    public MyShell()
    {
        InitializeComponent();

        Routing.RegisterRoute(nameof(Page2), typeof(Page2));
        Routing.RegisterRoute(nameof(Page1), typeof(Page1));

        this.Page1.IsEnabled = <Some Condition>;
    }
...
...
}

If disabled, Page2 should pop up first instead of Page1. Obviously, this is not the case automatically. So I changed the code to

bool enabled = this.Page1.IsEnabled = <Some Condition>;
if (!enabled)
     this.CurrentItem = Page2;

This worked for me, but after some GoToAsync(<Other Route>) , GoToAsync("..") causes error

System.ArgumentException: 'Ambiguous routes matched for: //D_FAULT_FlyoutItem7/IMPL_Page2/Page2/Page1 matches found: //D_FAULT_FlyoutItem7/IMPL_Page2/Page2/Page1, //D_FAULT_FlyoutItem7/IMPL_Page2/Page2/Page1 Parameter name: uri'

Obviously, my solution to set this.CurrentItem is not ok.

What is the correct solution to make Page2 popup first if Page1 is disabled? Can I change the Flyout Items order?

Edit

Perhaps the problem is that Page2 both is part of the Flyout Menu and sometimes is reached via the route (GoToAsync("Page2")). Then GoToAsync("..") causes this error.

What should I do?


Solution

  • The problem was that I defined identical routes twice:

    1. In .XAML - the Route attribute defines (not uses) a route
    2. In .cs - identical routes are defined by Routing.RegisterRoute

    Solution: either rename Routes in XAML or delete definitions in .cs