Tuesday, 8 July 2014

Transfering multiple data between pages in Visual Studio

I will assume that you have installed the tools for working like Visual Studio 2012 ( or 2013 ) and that you’re on Windows 8 at least ( or 8.1 ) .
In this workshop we will be transfering multiple data between pages in our Windows Phone application . We will be using new elements in the XAML code and using C# Classes .
Go ahead and create a new project , then follow these steps :
We will be working with Angry Birds today and handel their transfer between pages , so we will create a C# class named Bird that we will use during all the workshop : Right click on the project –> Add –>Class
Capture
We will add now a new class called SharedInformation that will contain all the informations that pages will be sharing .
Capture

Now add a new page to your application : Right click on the project —>Add—>New Item —> Windows Phone Portrait Page , then name it SecondPage.xaml
We will start now handeling the transfer of the data :
We will add a new XAML element which is listbox
Capture
Then we need to add some C# code to be able to work with the listbox , let’s add this code in the MainPage.xaml.cs
Capture
Let’s explain now what we are doing :
This listbox will contain multiple images of angry birds , you will select more than one bird and navigate to the second page and in this second page you will find what you’ve already choosen .
So here’s how things work :
We are preforming a Binding on this listbox to get elements
The image we’re using has also a Binding to the proprety imagePath
The textblock is binded also to the proprety name .
The whole workshop is based on a single line of code that will handle the link and transfer of the data , this line of code is : birdList.datacontext= birds;
We will now add a big feature in Windows Phone which is the application bar , we will have a workshop about application bar and how we use it but it’s not a big problem you can just use the code written here and will understand the concept .
Capture
We’ve added some images in the Assets of the application and add an event on each click of each button of the application bar .
Now let’s add the code behind of these events .
Capture

In this event we will learn how to navigate between pages in Windows Phone , this navigation will happen with this line of code :
 NavigationService.Navigate(new Uri(“/SecondPage.xaml”, UriKind.RelativeOrAbsolute));
We will move now to the SecondPage.xaml and an event on the load of the application , we can add this using XAML :
Capture

You can notice in the last line that we’ve added Loaded which is an event for the phone application page load , we will add now the C# code of this event :
Capture

Before running this project , just make sure that you’ve added some pictures in the Assets because I’ve used the angry birds pictures here .
Save the project and run and enjoy transfering multiple data between these two pages .

No comments:

Post a Comment