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

Issues in Application Architecture - Passing Data to Pages

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
3/13/2011 10:39:09 PM
The possible use of pages as dialog boxes provokes two questions:
  • How do I pass data from a source page to a destination page?

  • How do I return data when going back to the original page?

Interestingly, a facility is provided specifically for the first item but not for the second. I’ll show you this facility and then look at more generalized solutions to the second problem.

The following project is called SilverlightPassData. It is very much like the first project in this article except that when MainPage navigates to SecondPage, it provides SecondPage with its current background color, and SecondPage initializes itself with that color.

Here’s the content area of MainPage.xaml, the same as in the previous program:

Example 1. Silverlight Project: SilverlightPassData File: MainPage.xaml (excerpt)
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Navigate to 2nd Page"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0 34"
ManipulationStarted="OnTextBlockManipulationStarted" />
</Grid>

I won’t show you the OnManipulationStarted override because it’s the same as in the previous program, but the ManipulationStarted event handler for the TextBlock is a bit enhanced:

Example 2. Silverlight Project: SilverlightPassData File: MainPage.xaml.cs (excerpt)
void OnTextBlockManipulationStarted(object sender, ManipulationStartedEventArgs args)
{
string destination = "/SecondPage.xaml";

if (ContentPanel.Background is SolidColorBrush)
{
Color clr = (ContentPanel.Background as SolidColorBrush).Color;
destination += String.Format("?Red={0}&Green={1}&Blue={2}",
clr.R, clr.G, clr.B);
}

this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));

args.Complete();
args.Handled = true;
}


If the Background brush of the ContentPanel is a SolidColorBrush, then the handler gets the Color and formats the red, green, and blue values into a string that is appended to the name of the destination page. The URI now looks something like this:

“/SecondPage.xaml?Red=244&Green=43&Blue=91”

You’ll recognize this as a common format of an HTML query string.

The SilverlightPassData project also contains a SecondPage class that is the same as the one in the first project except that the code-behind file contains an override of the OnNavigatedTo method:

Example 3. Silverlight Project: SilverlightPassData File: SecondPage.xaml.cs (excerpt)
protected override void OnNavigatedTo(NavigationEventArgs args)
{
IDictionary<string, string> parameters = this.NavigationContext.QueryString;

if (parameters.ContainsKey("Red"))
{
byte R = Byte.Parse(parameters["Red"]);
byte G = Byte.Parse(parameters["Green"]);
byte B = Byte.Parse(parameters["Blue"]);

ContentPanel.Background =
new SolidColorBrush(Color.FromArgb(255, R, G, B));
}

base.OnNavigatedTo(args);
}


You’ll need a using directive for the System.Windows.Navigation namespace for the NavigationEventArgs class.

The OnNavigatedTo method is defined by Page, the class from which PhoneApplicationPage derives. The method is called right after the page has been created. When OnNavigatedTo is called, the page’s constructor has already executed, of course, but not much else has happened.

The destination class can access the query strings used to invoke the page through the page’s NavigationContext property. This property is of type NavigationContext, a class that has only one public property named QueryString, which returns a dictionary that I’ve saved in a variable called parameters. The code here assumes that if the “Red” query string is present, the “Blue” and “Green” must exist as well. It passes all the strings to the Byte.Parse method and reconstructs the color.

Now as you navigate from MainPage to SecondPage, the background color remains the same. As you go back, however, that’s not the case. There is no built-in facility like the query string to return data from one page to another.

Other -----------------
- Issues in Application Architecture - Basic Navigation
- Sensors and Services - Using a Map Service
- Sensors and Services - Geographic Location
- Sensors and Services : A Simple Bubble Level
- Sensors and Services : Accelerometer
- Programming Windows Phone 7 : The Intricacies of Layout - The Mighty Grid
- Programming Windows Phone 7 : The Intricacies of Layout - The Canvas and Touch
- Programming Windows Phone 7 : The Intricacies of Layout - The Retro Canvas
- Programming Windows Phone 7 : The Intricacies of Layout - A Custom Vertical StackPanel
- Programming Windows Phone 7 : The Intricacies of Layout - A Single-Cell Grid Clone
 
 
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