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 (11 chapters)

Passing data between pages

It is often necessary to pass some data from one page to another. It can be accomplished easily using another version of the Navigate method, called on the Frame property, as explained in this recipe.

Getting ready

To complete this recipe, you need the project with two pages, represented by MainPage and ProductsPage classes. The first page should contain the button with the Our products caption, while the other--the list of products and categories as well as the Go back button. The structure of pages is presented as follows:

Getting ready

It is worth mentioning that the page with products requires an identifier of the category, which should be presented. Such an identifier needs to be passed as a parameter. To show products and subcategories from the root category, 0 is passed. If you want to open a particular category, its identifier is passed instead, as shown on the right-hand side of the image.

How to do it...

To prepare an example that shows how to pass data between pages, you need to perform the following steps:

  1. To pass an integer value (namely 0) from MainPage to ProductsPage, modify the code of the button_Click method in the MainPage.xaml.cs file, as follows:
            private void button_Click(object sender, RoutedEventArgs e) 
            { 
                Frame.Navigate(typeof(ProductsPage), 0); 
            } 
    

  2. To read the passed integer value, add the OnNavigatedTo method to the ProductsPage.xaml.cs file, as follows:
            protected override void OnNavigatedTo( 
                NavigationEventArgs e) 
            { 
                base.OnNavigatedTo(e); 
                int categoryId = (int)e.Parameter; 
                List<string> products = GetProductsByCategory(categoryId); 
            } 
    

Note

To keep simplicity of the example, the scenario of showing a list of subcategories and products from a given category is not presented. If you want to develop it, you can use one of the items controls, as described in Chapter 3, MVVM and Data Binding.

How it works...

Navigating to another page by passing a value is possible in a way that is similar to the approach presented in the previous recipe. However, in such a case, you should use another version of the Navigate method with two parameters. The second is an object that is passed to another page. Of course, it could be an object of any type, not only an integer value as shown in the example.

A bit more explanation is necessary in the case of reading the parameter in the target page. To do so, you need to override the OnNavigatedTo method, which has a parameter that is an instance of the NavigationEventArgs class. Such a class contains the Parameter property. However, it is not strongly typed, so you need to cast it to a suitable type, such as int, as shown in the following code snippet:

     int categoryId = (int)e.Parameter; 

There's more...

Apart from passing an integer, a floating point, or even string values, it is often necessary and convenient to pass more complex data, such as instances of user defined classes, between pages. This task can be achieved with the same approach as described in detail in the current recipe. Of course, you should not forget about specifying the values of particular properties of the class instance before passing it to another page as well as casting the parameter to a proper type in the code of the target page.

See also

  • The Navigating between pages recipe
  • The Handling the back button recipe
  • The Changing a default page recipe
  • The Modifying the back stack recipe