Windows Phone 7-data transfer between pages source-http://blog.csdn.net/dncts/article/details/6160067

Source: Internet
Author: User
Windows Phone 7-data transfer between pages classification: Windows Phone 7 441 people read comments (0) collect reports

There are roughly three ways to pass parameters during page navigation: querystring, global variable, and independent storage. This article only describes the first two methods of passing values.

I. querystringMethod

 

The following code is added to the button event handler in page 1:
[C-sharp]View plaincopy
  1. Private void btnmain_click (Object sender, routedeventargs E)
  2. {
  3. String target = "/subpage. XAML ";
  4. Target + = string. Format ("? Inputtext = {0} ", txtinput. Text );
  5. This. navigationservice. navigate (New uri (target, urikind. Relative ));
  6. }
When the program runs, the URI is like this: "/subpage. XAML? Inputtext = Hello, Windows Phone 7 !" Its form and query string in HTML
(Query string), if you want to pass multiple parameters, you can use & to merge them, for example:
"/Subpage. XAML? Inputtext = Hello, Windows Phone 7! & Name = Zhang Yin ". The click event handler code of the button in page 2 is as follows:
[C-sharp]View plaincopy
  1. Private void btnback_click (Object sender, routedeventargs E)
  2. {
  3. If (this. navigationservice. cangoback)
  4. This. navigationservice. Goback ();
  5. }
In addition, I wrote the onnavigatedto method in page 2. The onnavigatedto method is defined in the page class,
Therefore, phoneapplicationpage also inherits this method, which is a virtual method, so it needs to be rewritten with the override keyword.
Onnavigatedto will be called immediately after the page is created. That is to say, when the onnavigatedto method is called, the page constructor has been executed.
Similarly, the onnavigatedfrom method is triggered when the current page is left. The code in the onnavigatedto method in page 2 is as follows:
[C-sharp]View plaincopy
  1. Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
  2. {
  3. String value = string. empty;
  4. Idictionary <string, string> querystring = This. navigationcontext. querystring;
  5. If (querystring. containskey ("inputtext "))
  6. {
  7. TXT. Text = querystring ["inputtext"];
  8. }
  9. Base. onnavigatedto (E );
  10. }
The page class defines a navigationcontext attribute of the navigationcontext type, which contains
Querystring attribute. After empty checking, assign the value corresponding to the inputtext key to the text attribute of the text box.
Finally, the onnavigatedto method of the base class is triggered to ensure that the function of the base class is normal. Of course, you can also use the following code, with the same effect:
[C-sharp]View plaincopy
  1. Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
  2. {
  3. String value = string. empty;
  4. Idictionary <string, string> querystring = This. navigationcontext. querystring;
  5. Querystring. trygetvalue ("inputtext", out value );
  6. If (value! = NULL)
  7. {
  8. TXT. Text = value;
  9. }
  10. Base. onnavigatedto (E );
  11. }
Although querystring can pass parameters to a page, it cannot return data. For example, I changed the text to "Windows Phone 7!" In page 2 !",
Click "Go to page 1st" to get the initial Page. To solve this problem, you need to use global variables.
Ii. Global Variables

For global variables, we can add a class that can be accessed throughout the life cycle of the program in the project and save the corresponding data in the attributes of the class.

But now we don't have to do this extra work on our own. The project itself has a class to meet this requirement: app class. The app class inherits from the application class.

The current attribute of the application can be used to obtain the application class instance associated with the current program, and then the app class instance can be obtained through conversion.

All pages in the program can access this app class, so it is best to put the data we want to save into the app class, eliminating the trouble of self-implementation.

Add a public inputtext attribute to the app class (the automatic attribute of C #3.0 ):

[C-sharp]View plaincopy
  1. Public partial class app: Application
  2. {
  3. Public String inputtext {Get; set ;}
  4. ......
  5. }

The code in page 1 is as follows:

[C-sharp]View plaincopy
  1. Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
  2. {
  3. App = application. Current as app;
  4. If (App. inputtext! = NULL)
  5. Txtpage1.text = app. inputtext;
  6. Base. onnavigatedto (E );
  7. }
  8. Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
  9. {
  10. App = application. Current as app;
  11. App. inputtext = txtpage1.text;
  12. Base. onnavigatedfrom (E );
  13. }

In the onnavigatedto method, I read the inputtext value of the app class, so that when the text is modified in page 2, it can be reflected in page 1.

Note:: The onnavigatedto method is called when the current page is activated (becomes an activity page.

In the onnavigatedfrom method, assign the text box to the inputtext attribute of the app class for page 2 to read.

The code in page 2 is as follows:

[C-sharp]View plaincopy
  1. Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
  2. {
  3. App = application. Current as app;
  4. If (App. inputtext! = NULL)
  5. Txtpage2.text = app. inputtext;
  6. Base. onnavigatedto (E );
  7. }
  8. Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
  9. {
  10. App = application. Current as app;
  11. App. inputtext = txtpage2.text;
  12. Base. onnavigatedfrom (E );
  13. }

The operations in page 2 are similar to those in page 1. In this way, when we change the text in page 2 to "Windows Phone 7 !", Then, when you click "Go to page 1st,

You can get the desired result.

 

Iii. phoneapplicationservice class

Similar to the app class, we can also save the data to be accessed on multiple pages in the state attribute of phoneapplicationservice.

Its type is idictionary <string, Object>, so we can save any object, but the premise is that this object is serializable ).

In the program, you do not need to create an instance by yourself. You can obtain an existing instance through the static attribute current of phoneapplicationservice.

Note: To access the phoneapplicationservice class in a program, you need to add the following namespace:

Using Microsoft. Phone. shell;

The program code is as follows. The code in page 1 is as follows:

[C-sharp]View plaincopy
  1. Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
  2. {
  3. If (phoneapplicationservice. Current. state. containskey ("inputtext "))
  4. {
  5. Txtpage1.text = phoneapplicationservice. Current. State ["inputtext"] as string;
  6. }
  7. Base. onnavigatedto (E );
  8. }
  9. Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
  10. {
  11. Phoneapplicationservice. Current. State ["inputtext"] = txtpage1.text;
  12. Base. onnavigatedfrom (E );
  13. }

The code in page 2 is as follows:

[C-sharp]View plaincopy
  1. Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
  2. {
  3. If (phoneapplicationservice. Current. state. containskey ("inputtext "))
  4. {
  5. Txtpage2.text = phoneapplicationservice. Current. State ["inputtext"] as string;
  6. }
  7. Base. onnavigatedto (E );
  8. }
  9. Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
  10. {
  11. Phoneapplicationservice. Current. State ["inputtext"] = txtpage2.text;
  12. Base. onnavigatedfrom (E );
  13. }

4. onnavigatedto and onnavigatedfrom Methods

I think the names of these two methods are not very good and sometimes confusing. If you change onnavigatedto to onnavigatedtothispagefromother,

Changing onnavigatedfrom to onnavigatedfromispaggatether will be clear. Of course, this is just a way to help us remember.

The following are some suggestions for the two methods in msdn:

Onnavigatedto: Override the onnavigatedto method to check the Navigation Request and prepare the page for display.

For example, you can load the requested data and enable or disable visualization elements.GenerallyOnnavigatedtoInstead Loaded Event creation event handler.

It is best to use the onnavigatedto method, because this method is called only once every time the page becomes an active page.

The Silverlight framework is available inEach time an element is added to a visual tree Loaded EventThis event may occur multiple times when a page is activated.

Onnavigatedfrom: Override the onnavigatedfrom method to perform the final operation on the page when the page becomes inactive.

For example, you can update data related to the page.GenerallyOnnavigatedfromInstead Navigated Event creation event handler.

It is best to use the onnavigatedfrom method, because you do not have to remove the event handler from the navigationservice object to avoid object Lifetime issues.

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.