使用ShellContent Xamarin表单时添加导航向后箭头 [英] add navigation back arrow when using ShellContent xamarin forms

查看:93
本文介绍了使用ShellContent Xamarin表单时添加导航向后箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,当我们使用ShellContent导航到仪表板页面时,导航栏中没有后退箭头? 您知道如何导航到仪表板页面并可以返回到上一页吗?

<Shell ..>
<ShellContent x:Name="home"
IsVisible={Binding IsVisibleHome}
Route="main"
ContentTemplate="{DataTemplate home:Dashboard}" />

<ShellContent Route="test"
ContentTemplate="{DataTemplate home:Dashboard2}" />

</Shell>

推荐答案

没有简单的内置方法可以做到这一点。

这里有一种方法,它将路由更改为不同的路由,该路由不会作为Shell的(ShellContent)子级进行访问。这基于AboutPage,该AboutPage是Xamarin窗体的";弹出&q;项目模板的一部分。将AboutPage替换为您的页面,并将路由替换为您的路由。

public partial class AppShell : Xamarin.Forms.Shell
{
    public AppShell()
    {
        InitializeComponent();
        // Define a route that isn't a child of Shell.
        Routing.RegisterRoute("about2", typeof(AboutPage));
    }

    protected override void OnNavigating(ShellNavigatingEventArgs args)
    {
        base.OnNavigating(args);

        if (args.Current != null) {
            // This line hopefully avoids interfering with Pop, if AboutPage links to another page.
            if (args.Source == ShellNavigationSource.ShellItemChanged) {
                // Replace "//AboutPage" with the route to your page.
                if (args.Target.Location.OriginalString == "//AboutPage") {
                    // Cancel the original route.
                    args.Cancel();
                    Device.BeginInvokeOnMainThread(() => {
                        // Go there by a route that isn't a child of Shell.
                        Shell.Current.GoToAsync("about2");
                    });
                }
            }
        }
    }
}

结果:以前的shell位置被推送到NAV堆栈。AboutPage出现,导航栏左上角有一个后退箭头。

换句话说,AboutPage现在的行为与以下任何其他页面一样:

  1. 未在Shell层次结构中定义-不是Shell的子级。
  2. 并且定义了路由。

诀窍是我们定义了第二条路由,它将我们带到同一个页面。然后我们截取原始路由,并将其替换为此备用路由。

这篇关于使用ShellContent Xamarin表单时添加导航向后箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆