【问题标题】:Bug? System.ArgumentException: 'unable to figure out route for:漏洞? System.ArgumentException: '无法找出路线:
【发布时间】:2020-11-25 10:23:21
【问题描述】:

错误

System.ArgumentException: '无法找出路线: //RegisterPage 参数名称:uri' System.ArgumentException: 'unable 找出路线: //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)}");
}

【问题讨论】:

  • 您是如何在主页&lt;Shell&gt; 中使用LoginPageRegisterPage 的?换句话说,您的页面的层次结构/结构是什么?您是否在Route 注册了您的页面?

标签: c# xamarin xamarin.forms xamarin.forms.shell


【解决方案1】:

您需要为您愿意使用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

【讨论】:

  • 当您尝试导航到具有 IsVisible="false" (Xamarin.Forms 4.8.0.1451) 的 flyoutitem 或 menuitem 时,也会引发此异常
  • @this.myself 我会测试它,你有没有用 5.0.0 预版本尝试过,如果它是固定的还是现在?或者如果一个问题已经被填满了?
  • 不,我没有使用 5.0 pre 进行测试,也没有查看是否有问题,因为我找到了解决问题的方法
  • 我可以确认,当我将 Xamarin.Forms 更新为 5.0.0.2337 时,“当您尝试导航到具有 IsVisible="false" 的 flyoutitem 或 menuitem 时也会引发异常”。确实存在/曾经有一个问题:github.com/xamarin/Xamarin.Forms/issues/12428
  • 这里是解释 5.0 docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/… TLDR 的“新”方式的文档链接:FlyoutItem.FlyoutItemIsVisible="False" not FlyoutItem.IsVisible="False"(FlyoutItem.IsVisible 是继承的来自 BaseShellItem 但目前在文档中没有关于它的描述)
猜你喜欢
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2020-02-15
相关资源
最近更新 更多