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:
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:
- To pass an integer value (namely
0
) fromMainPage
toProductsPage
, modify the code of thebutton_Click
method in theMainPage.xaml.cs
file, as follows:private void button_Click(object sender, RoutedEventArgs e) { Frame.Navigate(typeof(ProductsPage), 0); }
- To read the passed integer value, add the
OnNavigatedTo
method to theProductsPage.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