In the previous article, I briefly described how to transfer parameters by adding parameters after the URI. Sometimes it is not just to transfer data, but to share data. What should I do? The following describes three methods for data sharing.
Method 1: access public attributes
In the override protected override void onnavigatedfrom (system. windows. navigation. in the navigationeventargs e) method, parameter E contains a large amount of data. content indicates the page to be navigated. You can use E. content to access the public global variables on the page to be navigated. For example, (E. content as secondpage). textblock1.text = "DDD ";
Method 2: Use the app class
First, you must know that all pages in the program can access the app class derived from the application. You can add attributes and fields to the app class as shared data accessible to all pages. Access the static attribute current of the application class to return the application object of the current application. The current attribute returns a reference to the Application object instead of an instance of the class derived from the application. If you need to directly access the latter, You Need To forcibly convert the value returned by current to the type of the class derived from the application, such as (application. current as APP ). STR = "eee"; where STR is added to the app class: public partial class app: Application {Public String STR {set; get ;}}
Method 3: Use the state attribute of the phoneapplicationservice object
The state attribute of the phoneapplicationservice object is a dictionary interface of the idictionary <string, Object> type.
This project has two pages: mainpage and secondpage, each with three buttons and three textblocks. The Code is as follows:
View code
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 using Microsoft. Phone. shell;
14
15 namespace wpapp_navigation
16 {
17 // Add a public attribute for the class app. All pages in the program can access the app class derived from the application.
18 public partial class app: Application
19 {
20 Public String STR {set; get ;}
21}
22
23 public partial class mainpage: phoneapplicationpage
24 {
25 // Constructor
26 Public mainpage ()
27 {
28 initializecomponent ();
29 This. button1.click + = new routedeventhandler (button#click );
30}
31
32 void button#click (Object sender, routedeventargs E)
33 {
34 // do not leave spaces in the middle of the string parameter assigned to Uri, and use & to connect multiple parameters
35 This. navigationservice. navigate (New uri ("/secondpage. XAML", urikind. relativeorabsolute ));
36}
37
38 // override the onnavigatefrom method of the base class, triggered when the page is left
39 protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
40 {
41 // parameter E indicates the data provided for the Navigation Method
42 base. onnavigatedfrom (E );
43
44 // Method 1: access public attributes
45 // E. content indicates the target to which you are navigating.
46 If (E. Content is secondpage)
47 {
48 (E. content as secondpage). textblock1.text = "DDD ";
49}
50
51 // Method 2: using public attributes in the app class
52 // The current property returns a reference to the Application Object
53 (application. Current as APP). Str = "eee ";
54
55 // method 3: Use the state attribute of the phoneapplicationservice object
56 phoneapplicationservice. Current. State ["S6"] = "fff ";
57}
58
59 protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
60 {
61 base. onnavigatedto (E );
62
63 This. textblock2.text = (application. Current as APP). STR;
64
65 if (phoneapplicationservice. Current. state. containskey ("S6 "))
66 {
67 This. textblock3.text = (string) (phoneapplicationservice. Current. State ["S6"]);
68}
69
70}
71}
72}
View code
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 using Microsoft. Phone. shell;
14
15 namespace wpapp_navigation
16 {
17 public partial class secondpage: phoneapplicationpage
18 {
19 public int A = 0;
20 int B = 1;
21 public secondpage ()
22 {
23 initializecomponent ();
24 This. button1.click + = new routedeventhandler (button#click );
25}
26
27 void button#click (Object sender, routedeventargs E)
28 {
29 // return to the previous page
30 this. navigationservice. Goback ();
31}
32
33 // triggered when the page is an active page
34 protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
35 {
36 base. onnavigatedto (E); // call the virtual method of the base class.
37
38 This. textblock2.text = (application. Current as APP). STR;
39
40 if (phoneapplicationservice. Current. state. containskey ("S6 "))
41 {
42 This. textblock3.text = (string) phoneapplicationservice. Current. State ["S6"];
43}
44
45}
46
47 // triggered when the page is not an active page
48 protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
49 {
50 base. onnavigatedfrom (E );
51
52 // before leaving this page, the page can also pass data to the next page in three ways
53 If (E. Content is mainpage)
54 {
55 (E. content as mainpage). textblock1.texts = "123 ";
56}
57
58 (application. Current as APP). Str = "456 ";
59
60 phoneapplicationservice. Current. State ["S6"] = "789 ";
61}
62}
63}