Page navigation and data transfer for Windows Phone Learning

Source: Internet
Author: User

Create a Windows Phone application, add three buttons and three textblocks To The XAML file, and add a Windows Phone page (named secondpage ), add three buttons and three textblocks. The goal is to navigate from the mainpage page to the secondpage page, pass some parameters, and then return to the mainpage from secondpage.

The mainpage. XAML. CS code is as follows:

1 using system;
2 using system. Collections. Generic;
3 using system. LINQ;
4 using system. net;
5 using system. windows;
6 using system. Windows. controls;
7 using system. Windows. documents;
8 using system. Windows. input;
9 using system. Windows. Media;
10 using system. Windows. Media. animation;
11 using system. Windows. shapes;
12 using Microsoft. Phone. controls;
13
14 namespace wpapp_navigation
15 {
16 public partial class mainpage: phoneapplicationpage
17 {
18 // Constructor
19 public mainpage ()
20 {
21 initializecomponent ();
22
23 This. button1.click + = new routedeventhandler (button#click );
24}
25
26 void button#click (Object sender, routedeventargs E)
27 {
28 // string Path = "/secondpage. XAML ";
29 // string target = path + String. Format ("? S1 = {0} "," zou ");
30
31 // do not leave spaces in the middle of the string parameter assigned to Uri, and use & to connect multiple parameters
32 This. navigationservice. navigate (New uri ("/secondpage. XAML? S1 = aaa & S2 = BBB & S3 = CCC ", urikind. relativeorabsolute ));
33}
34}
35}

 

Code this. navigationservice. navigate (New uri ("/secondpage. XAML? S1 = aaa & S2 = BBB & S3 = CCC ", urikind. relativeorabsolute); is the core

Create a URI object. The/secondpage In the first parameter of the URI. XAML indicates the path of the page to be navigated. The question mark is followed by parameters to be passed in the past. S1, S2, and S3 are parameter names, and the equal sign is followed by their values, parameters are connected with &; urikind, the second parameter of Uri. relativeorabsolute indicates whether the preceding path is relative or absolute. relativeorabsolute indicates that the path may be relative or absolute.

 

The Code of secondpage. XAML. CS is as follows:

 

1 using system. Windows. Media. animation;
2 using system. Windows. shapes;
3 using Microsoft. Phone. controls;
4
5 namespace wpapp_navigation
6 {
7 public partial class secondpage: phoneapplicationpage
8 {
9 Public secondpage ()
10 {
11 initializecomponent ();
12 This. button1.click + = new routedeventhandler (button#click );
13
14}
15
16 void button#click (Object sender, routedeventargs E)
17 {
18 // return to the previous page
19 this. navigationservice. Goback ();
20}
21
22 // triggered when the page is an active page
23 protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
24 {
25 base. onnavigatedto (E); // call the virtual method of the base class.
26
27 if (navigationcontext. querystring. containskey ("S1 "))
28 {
29 This. textblock1.text = navigationcontext. querystring ["S1"];
30 this. textblock2.text = navigationcontext. querystring ["S2"];
31 This. textblock3.text = navigationcontext. querystring ["S3"];
32}
33}
34
35 // triggered when the page is not an active page
36 protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
37 {
38 base. onnavigatedfrom (E );
39 MessageBox. Show ("Leave secondpage ");
40}
41
42
43 // public void get ()
44 //{
45 // you cannot call navigationcontext. querystring in the method you write.
46 // idictionary <string, string> text = navigationcontext. querystring;
47 //}
48
49}
50}

Here, override the onnavigatedto method of the base class: protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)

The onnavigatedto method is triggered when the current page becomes active. Here, you can use navigationcontext. querystring to obtain the parameters passed from mainpage. Navigationcontext. querystring is of the idictionary <string, string> type, that is, a key/value pair.

To return to the mainpage, you can use this. navigationservice. Goback ();

When leaving the secondpage, if we want to do the last thing before leaving, we can put it in the onnavigatedfrom function, which is also implemented by rewriting the virtual method of the base class, when the page changes from activity to not activity page, protected override void onnavigatedfrom (system. windows. navigation. navigationeventargs E)

 

 

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.