Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain current page name in Xamarin Forms app

I am currently trying to understand how to get the name of the (xaml) page I am currently into, with my Xamarin Form app.

How am I supposed to do it? I tried a variety of cases, even looking around the Internet, but nothing actually worked for me so far :/

like image 940
Guido Magrin Avatar asked Dec 04 '14 22:12

Guido Magrin


2 Answers

This is just C# reflection - the name of the XAML page should match the name of it's class

var name = this.GetType ().Name;
like image 80
Jason Avatar answered Sep 21 '22 02:09

Jason


You would (commonly) use wither a one page app (master/detail, tabs page) or a set of navigable pages, managed by a NavigationPage. App.Current.MainPage would (normally) contain the first page shown and if it has .Navigation that would be the NavigationPage that can give you the most recently shown page - last one in the stack. If it doesn't you could assume your app being one page app.

All of the above is just "common" and not necessarily true in all cases, but gives you a starting point where to look for your current page.

var actionPage = App.Current.MainPage;
if (actionPage.Navigation != null)
    actionPage = actionPage.Navigation.NavigationStack.Last();
actionPage.DisplayActionSheet(...)
like image 25
Sten Petrov Avatar answered Sep 21 '22 02:09

Sten Petrov