バグ?System.ArgumentException: '次のルートを特定できません:

Nov 25 2020

エラー

System.ArgumentException: '次のルートを特定できません:// RegisterPageパラメーター名:uri' System.ArgumentException: '次のルートを特定できません:// LogoPageパラメーター名: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)}");
}

回答

3 Cfun Nov 25 2020 at 10:40

を使用してナビゲートする各ページのルートを登録する必要があります。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));

Microsoftドキュメント

詳細:シェルナビゲーション