当我们使用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
,它是 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
现在的行为与任何其他页面一样:
- 未在 Shell 层次结构中定义 - 不是 Shell 的子级。
- 并定义了一条路线。
“技巧”是我们定义了第二条路线,将我们带到同一页面。然后我们拦截了原来的路线,并用这条替代路线替换了它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)