During WP development, you may need to convert values between pages. If two pages are defined, one is the initial Page Source Page, and the other is the redirected page destination Page, simple analysis has two main requirements:
- The first is the implementation of transferring values to the destination Page when the Source Page jumps to the destination Page;
- Then, how to pass the value on the Source Page when the Goback function is called in the destination Page to return to the source page;
First, the system provides the basic implementation method to create a project datapassingdemo, and then create a page secondpage. in XAML, we need to jump from mainpage to secondpage, and pass the parameters for secendpage capture. First, add a textblock to mainpage and add the event handler function:
<Grid X: Name = "contentpanel" grid. row = "1" margin = "12, 0, 12, 0 "> <textblock text =" navigate to 2nd page with data "horizontalalignment =" center "verticalignment =" center "padding =" 0 34 "manipulationstarted =" login "/> </ grid>
In the mainpage backgroundCodeThe textblock_manipulationstarted method is as follows:
Private void textblock_manipulationstarted (Object sender, manipulationstartedeventargs ARGs) {string Destination = "/secondpage. XAML? Parameter1 = Hello & parameter2 = World "; this. navigationservice. navigate (New uri (destination, urikind. Relative); args. Complete (); args. Handled = true ;}
Can we see that the destination above is similar to the form of passing parameters between webpages? Similarly, add a textblock to secondpage and add a Goback () event to the manipulationstarted event of the textblock. In addition, in order to capture the parameters passed by mainpage, the following code is implemented in the background code of secondpage:
Protected override void onnavigatedto (system. windows. navigation. navigationeventargs ARGs) {idictionary <string, string> parameters = This. navigationcontext. querystring; If (parameters. containskey ("parameter1") {string parameter1 = parameters ["parameter1"]; string parameter2 = parameters ["parameter2"]; txtblk. TEXT = string. format ("parameter1 is: {0} And parameter2 is: {1}", parameter1, parameter2);} base. onnavigatedto (ARGs );}
You can use the onnavigatedto function to obtain the passed parameters and display them in a textblock.
Therefore, the method to implement the first value transfer request is very simple. You only need to attach the parameter to the target page address of navigationservice and then obtain the parameter from the target page. However, note that, because of the convenience requirements of mobile device design, we should avoid the design of complicated transfer parameters, and pay attention to the tombstone mechanism in the design of Windows Phone, in order to design a reasonable and efficient WP application.
Next we will consider the second question: how to share and transfer data between pages. We can consider that if there is a "Container" in the middle that can store some public data, Isn't that possible? If you are familiar with the Silverlight design, the app class will be displayed in your mind, because all pages can access the app class, therefore, we can define some data to be shared in the app class. In the above example, we add a public variable to the app class:
Public String sharedstring {set; get ;}
If you want to pass parameters to secondpage in mainpage, You need to first access the shared data. The background code in mainpage is as follows:
Private void textblock_manipulationstarted (Object sender, manipulationstartedeventargs ARGs) {(application. current as APP ). sharedstring = "Hello World"; this. navigationservice. navigate (New uri ("/secondpage. XAML ", urikind. relative); args. complete ();}
That is, modify the value of the shared data before accessing secondpage, and then modify the code in the onnavigatedto event of secondpage as follows:
Protected override void onnavigatedto (system. windows. navigation. navigationeventargs ARGs) {string sharedstring = (application. current as APP ). sharedstring; txtblk. TEXT = sharedstring; base. onnavigatedto (ARGs );}
Similarly, if you want to transmit data to mainpage through secondpage, you only need to modify the value of the shared data before calling the Goback function, and then the onnavigatedto function in mainpage can obtain the corresponding data.
Here we can basically implement the above two requirements, but the second method is just a clever method, which is unreasonable in both logic and implementation, we should consider another more reasonable and general implementation method, that is, the onnavigatedfrom function. You may think, isn't the from obvious? We just jump to the target page through the original page of The from, so what is the use of this from. In fact, it is very useful. For example, we can solve the problems mentioned above through this function.
Finally, we use an example to illustrate the specific implementation of this method. We define two pages, which are similar to the previous one. This time, we use the value returned by secondpage to define the color of the mainpage page, the background code of mainpage is defined as follows:
Public partial class mainpage: phoneapplicationpage {public mainpage () {initializecomponent ();} public color? Returnedcolor {set; get;} private void textblock_manipulationstarted (Object sender, manipulationstartedeventargs ARGs) {This. navigationservice. navigate (New uri ("/secondpage. XAML ", urikind. relative); args. complete (); args. handled = true ;}}
Color ?, The returned value may be non-color. The background code in secondpage is defined as follows:
Namespace datapassingdemo {public partial class secondpage: phoneapplicationpage {public secondpage () {initializecomponent ();} random Rand = new random (); Private void textblock_manipulationstarted) {This. navigationservice. goback (); args. complete (); args. handled = true;} protected override void onnavigatedfrom (system. windows. navigation. navigationeventargs e) {If (contentpanel. background is solidcolorbrush) {color CLR = (contentpanel. background As solidcolorbrush ). color; If (E. content is mainpage) (E. content as mainpage ). returnedcolor = CLR;} base. onnavigatedfrom (E);} protected override void onmanipulationstarted (manipulationstartedeventargs ARGs) {contentpanel. background = new solidcolorbrush (color. fromargb (255, (byte) Rand. next (255), (byte) Rand. next (255), (byte) Rand. next (255); base. onmanipulationstarted (ARGs );}}}
We can set the color combined by a random number to the background color of secondpage, and then set returnedcolor to the current background color through onnavigatedfrom. Therefore, in order to obtain the returnedcolor returned by secondpage, in the mainpage background code, you also need to reload the onnavigatedto method to respond to this onnavigatedfrom:
Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs ARGs) {If (returnedcolor! = NULL) contentpanel. Background = new solidcolorbrush (returnedcolor. Value); base. onnavigatedto (ARGs );}
Through onnavigatedfrom and onnavigatedto, we have completed the data transmission process.