xamarin 错误?系统参数异常:'无法确定的路由:

hivapdat  于 2023-02-14  发布在  其他
关注(0)|答案(2)|浏览(160)

错误
System.ArgumentException:'无法确定的路由://参数名称:uri '系统参数异常:'无法确定的路由://LogoPage参数名称:乌里
怎么了?它看不清路线......?
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)}");
}
pnwntuvh

pnwntuvh1#

您需要使用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));

微软文档

有关详细信息:髋臼杯导航

jmo0nnb3

jmo0nnb32#

对于我的问题,我在ShellPage后面注册了路由,花了一段时间才找到并添加新路由。我不确定这是在AppShell.cs中注册路由的最佳方式

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
        Routing.RegisterRoute(nameof(PartPage), typeof(PartPage)); 
        Routing.RegisterRoute(nameof(DetailPage), typeof(DetailPage));
        Routing.RegisterRoute(nameof(PersonPage), typeof(PersonPage));
    }
}

相关问题