Based on a lot of data, there are four main methods to pass parameters:
1. Use the querystring method of navigationcontext; 2. Use the app class of the program to set global variables (this method can pass objects); 3. Use the content attribute setting of the navigationeventargs event class; 4. Use the state attribute of the phoneapplicationservice class.
1. Use the querystring method of navigationcontext
This parameter passing method is the simplest and easiest to understand, similar to the question mark? Id = 1 type. First, add parameters to the URI of page B, such as "/View/music. XAML? Id = 1 "; it can be received on page B, for example, int id = int. parse (navigationcontext. querystring ["ID"]); Note: (1) You must determine whether a parameter exists before receiving it. (2) You can use "&" to connect multiple parameters. (3) This method can also be used for address hit. "{}" is used to represent the variable. For example, <NAV: urimapping
Uri = "music/{ID }"
Mappeduri = "/View/music. XAML? Id = {ID} "> </NAV: urimapping>
After this setting, "music/1" is equivalent to "/View/music. XAML? Id = 1 ".
2. Set global variables through the app class of the Program (this method can pass objects );Because the app class inherits from the application class, the application class instance associated with the current program can be obtained through the current attribute of the application, and then the app class instance can be obtained through conversion. Therefore, you can access any other page of the program by setting global variables in the app class. (1) In the app. set the global variable: public partial class app: Application {public int ID {Get; Set ;}( 2) in the XAML app class: app = application. current as app; app. id = 1; (3) received in page2: app = application. current as app; int id = app. ID; supplement: This method can also pass the object through the static attributes of the app class: (1) first, we create a new musicclass class: public class musicclass {public string name {set; get;} Public String file {set; get;} (2) Add static attributes to the app class: public static musicclass music {Get; set;} (3) set parameters in page1: app. music = new musicclass {file = "song1.mp3", name = "1"}; (4) received parameter in page2: app. music = new musicclass string musicfile = app. music. file; string musicname = app. music. name;
3. Use the content attribute settings of the navigationeventargs event class;In the onnavigatedfrom function of navigation to other pages, test whether the target page of the navigation is the page that you really want to switch to. If yes, you can use the navigationeventargs event class to inject some "passing content" to the target page ". Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
{
VaR targetpage = E. content as page2;
If (targetpage! = NULL)
{
Targetpage. ID = 1;
}
}
Then page2 retrieves the parameter value: Public int ID {Get; set ;}
Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
{
If (ID! = NULL)
{
Int id = ID. tostring ();
}
}
4. Use the state attribute of the phoneapplicationservice class.Because the state of the phoneapplicationservice class is an idictionary type, any object can be stored, but this object must be serializable. Note: The phoneapplicationservice class needs to access the namespace using Microsoft. phone. shell; in the program, you do not need to create your own phoneapplicationservice instance. You can obtain the existing phoneapplicationservice instance through the static attribute current of phoneapplicationservice.
Set the parameter in page1: protected override void onnavigatedfrom (system. windows. navigation. navigationeventargs e) {phoneappservice. state ["ID"] = int. parse (mytextbox. text); // obtain the page1 page value for transmission; base. onnavigatedfrom (E );}
Retrieve the parameter in page2: protected override void onnavigatedto (system. windows. navigation. navigationeventargse) {If (phoneapplicationservice. current. state. containskey ("ID") {mytextblock. TEXT = phoneapplicationservice. current. state ["ID"] as string;} base. onnavigatedto (E );}
Note: This method is generally not used to pass parameters. This method is usually used to remember the current status of the page and display the content being edited after the jump back.
Reference: http://blog.csdn.net/wangbole/article/details/7174663 http://www.cnblogs.com/elecpiano/archive/2011/11/14/2248378.html