Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Windows Phone 7 Execution Model : Passing Data Through Pages

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/3/2011 4:19:45 PM

1. Problem

You need to pass data from one page to another.

2. Solution

You can use the QueryString collection provided by the NavigationContext property from the PhoneApplicationPage class.

3. How It Works

The source page that has to pass parameters to the destination page can use the URL parameters' syntax:

/Page.xaml?param1=value&param2=value&...

NOTE

According to the official documentation at http://msdn.microsoft.com/en-us/library/z6c2z492(v=VS.95).aspx, you will receive an exception when the length of the QueryString parameter exceeds 65,519 characters.

After the page name, the parameters' definition starts with a question mark and includes a series of key/value pairs separated by the ampersand character.

To retrieve the parameters' values in the destination page, you have to use the QueryString collection provided by the NavigationContext property of the PhoneApplicationPage class.

4. The Code

To demonstrate navigation between pages with the exchange of parameters, you have to create a new Windows Phone 7 Silverlight application and call it NavigatingWitParamsApp. As you did in Recipe 2-1, add a new page and call it Page2.xaml.

In MainPage.xaml, you have to add three hyperlink buttons. You then define for each of them a Click event handler. You can take advantage of the TabIndexPage2. Let's see the XAML code of the MainPage page: property to set the value you want to pass to

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Orientation="Vertical" >
<HyperlinkButton Content="Option 1" Click="HyperlinkButton_Click"
TabIndex="1" />
<HyperlinkButton Content="Option 2" Click="HyperlinkButton_Click"
TabIndex="2" />
<HyperlinkButton Content="Option 3" Click="HyperlinkButton_Click"
TabIndex="3" />
</StackPanel>
</Grid>


In the code of the Click event handler, you can call the Navigate method, passing the parameter that represents the index of the pressed hyperlink button:

private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
HyperlinkButton hb = sender as HyperlinkButton;
string url = string.Format("/Page2.xaml?ID={0}", hb.TabIndex);
this.NavigationService.Navigate(new Uri(url, UriKind.Relative));
}


The sender object passed to the Click event handler represents the hyperlink button that raised the event. You can cast it to a HyperlinkButton object so you can use its TabIndex property and pass it to the Page2 page.

In the Page2.xaml file, you add a TextBlock control that will show a message indicating which button has been pressed in the MainPage page. As you can see in the following code, it is worth noting the PhoneTextTitle3Style static resource that is part of the theme resources for Windows Phone (see Recipe 3-5).

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock x:Name="lbText" Style="{StaticResource PhoneTextTitle3Style}" />
</Grid>


In the Page2.xaml.cs file, you have to add the Page_Loaded event handler, where you will retrieve the parameter value passed by the source page.

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
lbText.Text = string.Format("You clicked the option number {0}",
this.NavigationContext.QueryString["ID"]);
}


The Loaded event is raised when the page is fully created and every control is loaded. So you are sure here that the TextBlock is ready to show the Text property you set to the message. The event handler name must be specified in the XAML code of Page2.xaml as well:

<phone:PhoneApplicationPage
x:Class="NavigatingWithParamsApp.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"


FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">

. . .

5. Usage

From Visual Studio 2010, make sure your target is Windows Phone 7 Emulator and press Ctrl+F5. The emulator will start, briefly showing the splash screen and then the main page (see Figure 1).

Figure 1. The source page of the NavigatingWithParamsApp application

Select one option of your choice, and the application will show the Page2 page with a message telling which option you selected. For example, in our case we clicked Option 2; Figure 2 shows the related message.

Figure 2. The second page shows the index of the clicked hyperlink button.
Other -----------------
- Windows Phone 7 Execution Model : Navigating Between Pages
- Developing for Windows Phone 7 and Xbox 360 : Using the Content Pipeline - Content Importers
- Developing for Windows Phone 7 and Xbox 360 : Using the Content Pipeline - Content Processors
- Developing for Windows Phone 7 and Xbox 360 : Introduction to Custom Effects - Effect States
- Creating a Trial Windows Phone 7 Application
- Deploying the Windows Phone 7 Application on the Device
- Deploying the Application to the Windows Phone 7 Emulator
- Creating a Simple XNA Windows Phone 7 Application
- Creating a Simple Silverlight Windows Phone 7 Application
- Developing for Windows Phone 7 and Xbox 360 : Lighting (part 5) - Point Lights
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server