Search code examples
maui

How to determine the name of the page navigated from?


In the view model of a page, I'm trying to determine the name of the page the user came from. It looks like the navigation stack is just an array of Page's but for some reason, the following code is showing the red lines where I use nameof() to get the name of page from navigation stack.

var stack = Shell.Current.Navigation.NavigationStack.ToArray();
var indexForPreviousPage = stack.Count() - 2;
if(nameof(stack[indexForPreviousPage]) == nameof(SomePageInMyApp))
{
   // Do something
}

How do I get the name of the page the user came from? Is there an easier way to get it rather than look into the navigation stack?


Solution

  • You can't get name of your page using nameof(). Since your page is instance of Page type. What you can do is cast the page to it's type and then check it.

       if(stack[indexForPreviousPage] is SomePageInMyApp myPage)
       {
          // Do stuff using page
       }