【问题标题】:Xamarin Forms Shell navigate back with GoToAsync("..") causes exceptionXamarin Forms Shell 使用 GoToAsync("..") 向后导航导致异常
【发布时间】:2020-07-06 14:48:33
【问题描述】:

我有一个非常简单的导航堆栈,由一个根页面和一个位于其顶部的模型页面组成。我的根页面被设置为根页面,成为向 Shell 注册的第一个页面。

    public AppShell()
    {
        InitializeComponent();

        // opening view
        Regester<LoginPage>();

        Regester<SignUpPage>();
        ...

    }

    private void Regester<T>()
    {
        System.Diagnostics.Debug.WriteLine($"Regestering with shell : {typeof(T).Name} - {typeof(T)}");
        Items.Add(new ShellContent { Route = typeof(T).Name, ContentTemplate = new DataTemplate(typeof(T)) });
        Routing.RegisterRoute(typeof(T).Name, typeof(T));
    }
}

然后我使用相对路由导航到模型注册页面

Shell.Current.GoToAsync("SignUpPage");

否则我将使用绝对路由

  Shell.Current.GoToAsync("///LogInPage/SignUpPage");

然后我将尝试在堆栈中导航回使用

Shell.Current.GoToAsync("..");

但我得到了这个异常

Global routes currently cannot be the only page on the stack, so absolute routing to global routes is not supported. For now, just navigate to: LoginPage/

出现异常时,CurrentState.Location 属性是这个

Location = {///LoginPage/SignUpPage}

我不明白为什么会这样。如果我进一步深入堆栈并执行诸如尝试从详细信息页面导航回来之类的操作,也会发生这种情况。我需要做什么才能正确使用 GoToAsync("..")?

【问题讨论】:

    标签: xamarin.forms navigation xamarin.shell


    【解决方案1】:

    原因:

    您的路径///LogInPage 无效。全球路线目前不能是导航堆栈上的唯一页面。因此,不支持到全局路由的绝对路由。

    解决方案:

    还可以通过将有效的相对 URI 指定为 GoToAsync 方法的参数来执行导航。路由系统将尝试将 URI 与 ShellContent 对象匹配。因此,如果应用程序中的所有路由都是唯一的,则只需将唯一的路由名称指定为相对 URI 即可进行导航:

    await Shell.Current.GoToAsync("SignUpPage");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 2020-06-20
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多