【问题标题】:How can I obtain current Shell Content Page reference in Xamarin App Shell?如何在 Xamarin App Shell 中获取当前的 Shell 内容页面引用?
【发布时间】:2019-09-05 10:09:46
【问题描述】:

我正在使用 App Shell 开发 Xamarin Forms 应用。

我发现我必须在 AppShell.xaml.cs 中为 ShellContent Page 处理 android 硬件后退按钮,而不是 ShellContent Page。

protected override bool OnBackButtonPressed()
{
  //Get currernt shell content and call its method
  //like (Shell.Current.Content as MyContentPage).MyMethod();
}

1) 如何在 AppShell.xaml.cs 中获取当前的 Shell 内容页面引用?

2) 如何在没有路由的情况下以编程方式更改当前的 Shell 内容页面?

最好的问候。

【问题讨论】:

  • 可以使用 MessaginCenter 调用 MyMethod() docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
  • 为什么您需要确切知道正在显示哪个 ContentPage?
  • 我还看到你想调用 MyMethod,如果我知道它的作用会有所帮助!
  • 我添加了一个包含 WebView 控件的内容页面。当我按下 android 硬件按钮时,在 AppShell.xaml.cs 中调用 OnBackButtonPressed,而不是在内容页面中调用。我想在按下 android 后退按钮时调用 WebViewControl.GoBack 方法,而不是关闭我的应用程序。因为在 AppShell.xaml.cs 中调用了 OnBackButtonPressed,所以我必须获取当前的 ShellContent 实例并获取 WebViewControl 并调用其 GoBack 方法。
  • 这个问题你解决了吗?

标签: xamarin xamarin.forms


【解决方案1】:

我花了很多时间挖掘,但我找到了一个满足这个需求的公共 Shell 接口。如果您将ShellSection 转换为IShellSectionController,则可以使用PresentedPage 属性获取当前的Page 对象。下面是一个代码sn-p,YMMV。

(Shell.Current?.CurrentItem?.CurrentItem as IShellSectionController)?.PresentedPage

【讨论】:

  • 终于!我花了一些时间寻找这个解决方案.. 非常感谢您
【解决方案2】:

在我的情况下,我有带有外壳的底部选项卡,并且想在另一个选项卡上从选项卡中获取值,我修改了@Nick W 答案以使用:

(Shell.Current.CurrentItem.Items[0] as IShellSectionController).PresentedPage

其中索引 0 指的是 Tabbar 集合中的第一页。

【讨论】:

    【解决方案3】:

    我知道这个答案有点晚了,但我注意到现在有一​​个比目前提出的解决方案更简单的解决方案:

    Shell.Current?.CurrentPage;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      相关资源
      最近更新 更多