Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I obtain current Shell Content Page reference in Xamarin App Shell?

I am developing a Xamarin Forms app using App Shell.

I found that I had to handle android hardware back button in AppShell.xaml.cs for a ShellContent Page, instead of ShellContent Page.

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

1) How can I obtain current Shell Content Page reference in AppShell.xaml.cs?

2) How can I change current Shell Content Page programmatically without routing ?

Best regards.

like image 877
Ivoryguard Avatar asked Sep 05 '19 10:09

Ivoryguard


Video Answer


3 Answers

It took quite a bit of digging, but I found a public Shell interface that meets this need. If you cast your ShellSection as an IShellSectionController you can use the PresentedPage property get the current Page object. Below is a code snippet, YMMV.

(Shell.Current?.CurrentItem?.CurrentItem as IShellSectionController)?.PresentedPage
like image 111
Nick W. Avatar answered Oct 17 '22 07:10

Nick W.


I know this is a bit of a late answer, but I noticed that now there is a simpler solution than the solutions presented at the moment:

Shell.Current?.CurrentPage;
like image 3
Dennis van Opstal Avatar answered Oct 17 '22 07:10

Dennis van Opstal


In my case,I had bottom tabs with shell and wanted to get values from a tab when on a different tab, I modified @Nick W answer to achieve that using:

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

where index 0 refers to the first page in the Tabbar collection.

like image 1
Godfred Boateng Avatar answered Oct 17 '22 06:10

Godfred Boateng