如何在 Xamarin App Shell 中获取当前的 Shell 内容页面参考?

Ivo*_*ard 5 xamarin xamarin.forms

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

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

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

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

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

此致。

小智 14

费了不少功夫,但我找到了一个满足这种需求的公共 Shell 接口。如果您将您的ShellSectionas anIShellSectionController您可以使用该PresentedPage属性获取当前Page对象。下面是一个代码片段,YMMV。

(Shell.Current?.CurrentItem?.CurrentItem as IShellSectionController)?.PresentedPage
Run Code Online (Sandbox Code Playgroud)


Den*_*tal 8

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

Shell.Current?.CurrentPage;
Run Code Online (Sandbox Code Playgroud)