【问题标题】:add navigation back arrow when using ShellContent xamarin forms使用 ShellContent xamarin 表单时添加导航返回箭头
【发布时间】:2022-01-15 05:46:14
【问题描述】:

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

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

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

</Shell>

【问题讨论】:

  • 您能否重新表述并阐明所需的行为?
  • 完成,我使用 Shell.Current.GoToAsync() 时的问题,它添加到堆栈中,但是使用 ContentTemplate 没有 Shell.Current.GoToAsync() .. 没有添加堆栈

标签: c# xamarin.forms navigation


【解决方案1】:

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

这是一种方法,它将路由更改为不同的路由,该路由不作为 Shell 的 (ShellContent) 子级访问。这基于 AboutPage,它是 Xamarin 表单的“Flyout”项目模板的一部分。将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 位置被推送到导航堆栈。出现AboutPage,导航栏左上方有一个后退箭头。

换句话说,AboutPage 现在的行为类似于任何其他页面:

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

“诀窍”是我们定义了第二条路线,将我们带到同一页面。然后我们截取了原来的路线,换成了这条备用路线。

【讨论】:

  • 同意你在其他帖子中所说的它的愚蠢它对我来说就像这样任何离开第一页的导航都应该总是发布一个后退按钮。
猜你喜欢
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多