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.
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
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With