Book Image

Windows Application Development Cookbook

By : Marcin Jamro
Book Image

Windows Application Development Cookbook

By: Marcin Jamro

Overview of this book

Need to ensure you can always create the best Windows apps regardless of platform? What you need are solutions to the biggest issues you can face, so you can always ensure you’re making the right choices and creating the best apps you can. The book starts with recipes that will help you set up the integrated development environment before you go ahead and design the user interface. You will learn how to use the MVVM design pattern together with data binding, as well as how to work with data in different file formats. Moving on, you will explore techniques to add animations and graphics to your application, and enable your solution to work with multimedia content. You will also see how to use sensors, such as an accelerometer and a compass, as well as obtain the current GPS location. You will make your application ready to work with Internet-based scenarios, such as composing e-mails or downloading files, before finally testing the project and submitting it to the Windows Store. By the end of the book, you will have a market-ready application compatible across different Windows devices, including smartphones, tablets, and desktops.
Table of Contents (17 chapters)
Windows Application Development Cookbook
Credits
About the Author
Acknowledgements
About the Reviewer
www.PacktPub.com
Preface

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:

  1. 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(); 
            } 
    

  2. Run the project and navigate from MainPage to UpdatePage and to ResultsPage. Then, click on Go back and ensure that you have been navigated directly to MainPage, not to UpdatePage.

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