Modifying the back stack
While navigating from one page to another, the previous page is pushed to the back stack to allow the user to go back. However, in some scenarios, it could be necessary to programmatically read the back stack in order to know what the previously visited pages are. What is more, you can manually modify the back stack, for instance, to go back two pages instead of one. In this recipe, you will learn how to do it.
As an example, you will analyze the scenario presented in the following image:
On the dashboard (MainPage
), the user clicks on the Update data button. It navigates the user to UpdatePage
with the progress bar informing him/her about the progress of downloading necessary data. As soon as the download process is completed, the user is navigated to ResultsPage
with the Go back button. After pressing it, the user should go back directly to the dashboard, not to the previous page (UpdatePage
).
Getting ready
To use this recipe, you need a project with three pages, namely MainPage
, UpdatePage
, and ResultsPage
. On the first, you should place the Update data button navigating to UpdatePage
. Just as an example, the UpdatePage
could be equipped with the button, which will navigate the user to the last page. On ResultsPage
, let's place the Go back button. You should have an empty method handling the event of clicking on this button. Its code will be modified later in this recipe.
How to do it...
To modify the back stack to omit one page while going back, you need to perform the following steps:
Modify the body of the method handling clicking on the Go back button in the
ResultsPage.xaml.cs
file, as follows:if (Frame.BackStackDepth > 1) { Frame.BackStack.RemoveAt(Frame.BackStack.Count - 1); } if (Frame.CanGoBack) { Frame.GoBack(); }
Run the project and navigate from
MainPage
toUpdatePage
and toResultsPage
. Then, click on Go back and ensure that you have been navigated directly toMainPage
, not toUpdatePage
.
How it works...
At the beginning, you check whether the back stack has a suitable number of elements, which is at least two. Only in such a case, you are able to skip one entry while going back. If the mentioned condition is satisfied, the last entry from the back stack is removed using the following line of code:
Frame.BackStack.RemoveAt(Frame.BackStack.Count - 1);
Then, it is necessary to go back using the GoBack
method, already explained in one of the previous recipes in this chapter.
There's more...
The back stack performs a really important role while navigating between pages within an application. For this reason, it is beneficial to learn how it works and what information is stored in the back stack. You can check this with the following code snippet:
for (int i = 0; i < Frame.BackStack.Count; i++) { PageStackEntry entry = Frame.BackStack[i]; Type pageType = entry.SourcePageType; object pageParameter = entry.Parameter; }
This code fragment iterates through the back stack using the for
loop. In each iteration, you get a PageStackEntry
instance (named entry
) that contains various information about a visited page, such as its type and parameter.
Note
You can display a value while debugging using the code presented in the Logging information while debugging recipe.
In the exemplary scenario, analyzed in this recipe, the back stack contains two entries: regarding MainPage
(index 0) and UpdatePage
(index 1).
See also
The Navigating between pages recipe
The Passing data between pages recipe
The Handling the back button recipe
The Changing a default page recipe
The Modifying the back stack recipe