Home > Back-end >  How to access PreviousPage property in NavigatedToEventArgs in MAUI
How to access PreviousPage property in NavigatedToEventArgs in MAUI

Time:01-18

I am trying to determine which was the previous page for current page in .NET MAUI application.

For example, there are 3 pages Page1, Page2, Page3

When going from Page1 to Page2, in Page2, I would like to access PreviousPage property which gives me "Page1" as value.

When going from Page3 to Page2, in Page2, PreviousPage property gives me "Page3" as value.

^^ However, I can only see "PreviousPage" property member in Debug mode when VS hits breakpoint. I cannot access it in code. Intellisense does not show this too.

How can I access and use this "PreviousPage" in my code? Is there any other way?

See screenshot.

enter image description here

I am using: Microsoft Visual Studio Community 2022 (64-bit) - Preview Version 17.5.0 Preview 1.0

Thank you.

CodePudding user response:

https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation?view=net-maui-7.0

Navigation events The Shell class defines the Navigating event, which is fired when navigation is about to be performed, either due to programmatic navigation or user interaction. The ShellNavigatingEventArgs object that accompanies the Navigating event provides the following properties...

Technically you can implement your own custom logic, to see if something has been loaded, what was source, the navigation paths, etc...

Your requirement can be achieved with minimum amount of code.

However, I do not think that navigation stack is a healthy way to check if something is displayed on your page or not.

CodePudding user response:

You could get all the page in Navigation Stack, simply used:

var stack = Application.Current.MainPage.Navigation.NavigationStack;
int count = Application.Current.MainPage.Navigation.NavigationStack.Count;

The current page is the last one, names (count - 1) index (as it is zero-based index) in the stack, so the previous page is (count - 2) index.

Hope it works for you.

  • Related