Windows Phone development path (16) how to transfer data between pages

Source: Internet
Author: User

The problem solved in this article is how to transmit data from the source page to the target page.In fact, Windows Phone already provides us with a solution, that is, querying strings.

The following project will achieve the following effect: When you navigate from the mainpage page to secondpage, this project provides the current background color of mainpage for secondpage, and secondpage also initializes itself to this color. The data passed here is the background color value.

  Mainpage. XAML code:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Navigate to Second Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  Mainpage. xaml c # code:

Namespace silverlightpassdata
{
Public partial class mainpage: phoneapplicationpage
{
Random Rand = new random ();

// Constructor
Public mainpage ()
{
Initializecomponent ();
}

Private void textblock_manipulationstarted (Object sender, manipulationstartedeventargs e) // process the manipulation event
{
// Todo: navigate to the second page and pass the background color information to the second page.
String Destination = "/secondpage. XAML"; // create a string variable to save the target page address

If (this. contentpanel. background is solidcolorbrush) // check whether the image can be converted to the solidcolorbrush type. The background attribute is of the brush type.
{
Color CLR = (this. contentpanel. Background As solidcolorbrush). color; // convert the value of the background attribute to the solidcolorbrush type and obtain the color value.
Destination + = string. Format ("? Red = {0} & green = {1} & Blue = {2} ", CLR. r, CLR. g, CLR. b); // Add the color value as a parameter to the target address, which is similar to the HTML query string format.
}

This. navigationservice. navigate (New uri (destination, urikind. Relative); // navigate to the target address

E. Complete (); // indicates that other manipulation events will not be processed.
E. Handled = true; // mark the route event as handled
}

Protected override void onmanipulationstarted (manipulationstartedeventargs e) // override the virtual method of the base class control
{
// Todo: the background color of the page is randomly changed when the screen is clicked.
This. contentpanel. background = new solidcolorbrush (color. fromargb (255, (byte) Rand. next (256), (byte) Rand. next (256), (byte) Rand. next (256 )));

Base. onmanipulationstarted (E); // The base class access expression calls the base class method.
}
}
}

  

  Secondpage. XAML code:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Go back to Main Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  Secondpage. xaml c # code:

Namespace silverlightpassdata
{
Public partial class secondpage: phoneapplicationpage
{
Public secondpage ()
{
Initializecomponent ();
}

Private void textblock_manipulationstarted (Object sender, manipulationstartedeventargs e) // process the manipulation event
{
// Todo: return or navigate to the mainpage
This. navigationservice. Goback ();

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

Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs e) // rewrite the virtual method of the base class page. This method is called when the page changes to the active page of the framework.
{
// Todo: Get the data passed from mainpage and initialize the background color of the page.
Idictionary <string, string> parameters = This. navigationcontext. querystring; // create a generic interface dictionary to accept the returned result object

If (parameters. containskey ("red") // call the containskey () method to determine whether the result object contains the red key.
{
// Todo: Obtain the values of each key and convert them to the byte type.
Byte r = byte. parse (parameters ["red"]);
Byte G = byte. parse (parameters ["green"]);
Byte B = byte. parse (parameters ["blue"]);

This. contentpanel. Background = new solidcolorbrush (color. fromargb (255, R, G, B ));
}

Base. onnavigatedto (E); // call the base class virtual Method
}
}
}

Effect

The next article will summarize how to share data between pages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.