passing data between pages includes two questions
1, how to transfer data from the source page to the target page?
The following examples provide solutions to the above problems.
source page mainpage.xaml content area contains a TextBlock, such as
<textblock horizontalalignment= "center" name= "TXT1" text= "Navigate to 2nd Page" verticalalignment= "center" manipulationstarted= "txt1_manipulationstarted"/>
the
MainPage.xaml.cs code looks like this:
namespace PhoneApp2
{
public partial class Mainpage:phoneapplicationpage
{
Random rand = new Random ();
//constructor
public MainPage ()
{
InitializeComponent ();
}
private void txt1_manipulationstarted (object sender, ManipulationStartedEventArgs e)
{
String Destination = "/page1.xaml";
if (this. Contentpanel.background is SolidColorBrush)
{
Color clr = (this. 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));//Navigate to the specified page
e.complete ();
e.handled = true;
}
protected override void Onmanipulationstarted (ManipulationStartedEventArgs e)
{///when touching the TextBlock part of the page, the contentpanel background becomes random color.
This . Contentpanel.background = new SolidColorBrush (Color.FromArgb (255, (byte) rand. Next (255), (byte) rand. Next (255), (byte) rand. Next (255));/Set background color
Base. Onmanipulationstarted (e);
}
}
}
the target page Page1.xaml code overrides the Onnavigatedto method, as follows:
protected override void Onnavigatedto (System.Windows.Navigation.NavigationEventArgs e)//When the function is called, the The page's constructor has been executed, but no other method has been executed
{
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"]);
This . Contentpanel.background = new SolidColorBrush (Color.FromArgb (255,r,g,b));
}
Base. Onnavigatedto (e);
}
Running the program, navigating from MainPage to Page1, you will find that the background color is the same.
2, when back to the source page, how to return data?
windows Phone provides us with a solution to the first problem, but there is no ready-made solution to the second problem. The following example is a compromise proposal that can be referred to.
above MainPage.xaml.cs can be changed into
namespace PhoneApp2
{
public partial class Mainpage:phoneapplicationpage {
Random rand = new Random ();
public Color? Returnedcolor{set;get;} This is a nullable (nullable) color object that holds the data (color) you want to return
//constructor
public MainPage ()
{
InitializeComponent ();
}
private void txt1_manipulationstarted (object sender, ManipulationStartedEventArgs e)
{
String destination = "/page1.xaml";
this. Navigationservice.navigate (new Uri (destination, urikind.relative));//Navigate to the specified page
e.complete ();
e.handled = true;
}
protected override void Onmanipulationstarted (ManipulationStartedEventArgs e) The Contentpanel background changes to a random color when you touch a part of the page that is outside TextBlock.
{//.
this. Contentpanel.background = new SolidColorBrush (Color.FromArgb (255, (byte) rand. Next (255), (byte) rand. Next (255), (bYte) Rand. Next (255))//sets the background color
base. Onmanipulationstarted (e);
}
protected override void Onnavigateto (NavigationEventArgs e)//When the constructor is finished and no other functions have been executed (automatic) Call
{
if (this. Returnedcolor!= null)
{
this. Contentpanel.background = new SolidColorBrush (this. Returnedcolor.value);
}
base. Onnavigateto (e);
}
}
}
Page1.xaml.cs can be changed to
namespace PHONEAPP2
{
Public partial class page1:phoneapplicationpage {
Random rand = new Random ();
Public Color? Returnedcolor{set;get;} This is a nullable (nullable) color object that holds the data (color) that you want to return
//constructor
Public Page1 ()
{
InitializeComponent ();
}
private void Txt2_manipulationstarted (object sender, ManipulationStartedEventArgs e)
{
this. Navigationservice.goback ();//Navigation returns
E.complete ();
e.handled = true;
}
protected override void Onmanipulationstarted (ManipulationStartedEventArgs e)
{//When touching a part of the page other than TextBlock, The background of the Contentpanel will change to a random color.
this. Contentpanel.background = new SolidColorBrush (Color.FromArgb (255, (byte) rand. Next (255), (byte) rand. Next (255), (byte) rand. Next (255));//sets the background color
base. Onmanipulationstarted (e);
}
protected override void Onnavigatefrom (NavigationEventArgs e)
{
if (this. Contentpanel.background is SolidColorBrush)
{
Color clr = (this. Contentpanel.backgrOund as SolidColorBrush). Color;
if (E.content is MainPage)
(e.content as MainPage). Returnedcolor = clr;//Sets the return value when navigating out the Page1 page and saves it to the mainpage variable,
}
base. Onnavigatefrom (e);
}
}
}
-Parameter NavigationEventArgs class defines two properties: URI of Uri type and content of object type.
-When mainpage uses the parameter "/page1.xaml" to invoke the Navigate () function, the Onnavigatefrom () method in MainPage is invoked, and the parameter Uri property of the invocation is "/page1.xaml". The Content property type is Page1. This is a newly created Page1 instance of a person. The Onnavigateto () that is then invoked by Page1 also uses the same event arguments to identify the URI object with the value "/page1.xaml" and the Content object with the value Page1.