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.
I am using:
Microsoft Visual Studio Community 2022 (64-bit) – Preview
Version 17.5.0 Preview 1.0
Thank you.
3
Answers
Like @h-a-h mentioned, custom logic was the way. I implemented sort of workaround to make it work. I used navigation parameters.
My viewmodel constructor has attribute like this:
When going from Page1 to Page2, I do:
When going back to Page2 from Page3, I do:
At least this way I know "when" Page2 is visited so as to prevent loading of data again. Though this does not let me know if I am coming back to Page2 from Page3 or Page'n', it solves my current requirement.
One can always use query parameters to provide the parent page. e.g., from Page5 to Page2, we can do:
provided Page2's ViewModel has this QueryParameter attribute.
Not very elegant but workable.
Thank you all for your help.
https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation?view=net-maui-7.0
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.
You could get all the page in Navigation Stack, simply used:
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.