The possible use of pages as
dialog boxes provokes two questions:
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.