漏洞? System.ArgumentException: '无法找出路线:

Bug? System.ArgumentException: 'unable to figure out route for:

错误

System.ArgumentException: 'unable to figure out route for: //RegisterPage Parameter name: uri' System.ArgumentException: 'unable to figure out route for: //LogoPage Parameter name: uri'

怎么了?找不到路线...?

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="MyApp.Views.WelcomePage"
             Shell.NavBarIsVisible="False">
    <ContentPage.Content>
        <StackLayout Padding="10,0,10,0" VerticalOptions="Center">
            <Button VerticalOptions="Center" Text="Register" Command="{Binding RegisterCommand}"/>
            <Button VerticalOptions="Center" Text="Login" Command="{Binding LoginCommand}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

代码隐藏

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class WelcomePage : ContentPage
{
    public WelcomePage()
    {
        InitializeComponent();
        this.BindingContext = new WelcomeViewModel();
    }
}

WelcomeViewModel.cs

public Command RegisterCommand { get; }
public Command LoginCommand { get; }

public WelcomeViewModel()
{
    RegisterCommand = new Command(OnRegisterClicked);
    LoginCommand = new Command(OnLoginClicked);
}

private async void OnRegisterClicked(object obj)
{
    await Shell.Current.GoToAsync($"//{nameof(RegisterPage)}");
}

private async void OnLoginClicked(object obj)
{
    await Shell.Current.GoToAsync($"//{nameof(LoginPage)}");
}

您需要为您愿意使用 Shell.Current.GoToAsync() 导航到其中的每个页面注册一个路由,这样您还可以阐明您的页面层次结构:

    <FlyoutItem FlyoutDisplayOptions="AsMultipleItems">

        <ShellContent Title="RegisterPage"
                      Route="RegisterPage"
                      ContentTemplate="{DataTemplate local:RegisterPage}"/>

        <ShellContent Title="LoginPage"
                      Route="LoginPage"
                      ContentTemplate="{DataTemplate local:LoginPage}"/>

        <ShellContent Title="Page3"
                      ContentTemplate="{DataTemplate local:Page3}"/>

    </FlyoutItem>

如果愿意,您也可以在代码中使用 Routing.RegisterRoute() 注册路由,只要它在调用路由之前运行即可: Routing.RegisterRoute("//Page3", typeof(Page3));

微软文档

更多详情:Shell Navigation