Windows Phone 7 tip-parameter transfer between different pages

Source: Internet
Author: User

A friend asked me how to pass parameters between pages during page navigation on Windows Phone 7. Let's sort it out here.

There are three methods for passing parameters between pages on Windows Phone 7. To facilitate the description, we set the scenario to: navigate from page a to page B.

 

Method 1: querystring

This parameter passing method inherits from the traditional web development mode. In interface A, callIn the navigationservice. navigate () method, the URI passed is not only the XAML file address of page B, but also a string format parameter.The specific format is as follows:

 

"/Pageb. XAML? Param1 = stringvalue1 & param2 = stringvalue2"

 

The XAML file address is followed by a question mark, which indicates the beginning of the parameter declaration. Separate multiple parameters with the & symbol.

On the B page, you need to reload the onnavigatedto method of the base class and obtain the passed parameter values. For example:CodeAs follows:

String Paramvalue1 = navigationcontext. querystring [ " Param1 " ];

StringParamvalue2 = navigationcontext. querystring ["Param2"];  

This parameter passing method is easy to use. The only drawback is that only the string type is supported.

 

Method 2: Global Parameters

Define any type of public attributes (such as param3) in the app class in the app. XAML. CS file ).

On the page, set the property variable value. The Code is as follows:

 

(App. CurrentAsAPP). param3 = somevalue;

Access the variable on page B with the following code:

Somevariable = (App. Current As APP). param3;

This parameter passing method is flexible and supports global access.

 

Method 3: navigationeventargs. Content 

In fact, when you are about to leave page A, you can get the instance to go to the page (when you navigate to the page, first create the instance of page B, and then replace the current display content from page a with page B ). Therefore, we can find out whether the target page of the navigation is page B. If yes, we will "inject" some content to page B.

The code for page a is as follows:

Protected Override Void Onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
{
VaR Targetpage = E. Content As Pageb;
If (Targetpage! = Null )
{
Targetpage. param4 = somevalue;
}
}

 

The B Page code is as follows:

Public Paramtype param4 { Get ; Set ;}

Protected Override VoidOnnavigatedto (system. Windows. Navigation. navigationeventargs E)
{
If(Param4! =Null)
{
Textblock3.text = param4.name;
}

}

This method also supports various parameter types.

 

The above parameter transmission methods are superior or inferior.

Some people may also add that some frameworks (such as prism and mvvmlighttoolkit) also support more parameter transmission methods (such as messaging), but I personally feel that these frameworks are just "airplane carriages". it is not suitable for small-scale mobile app development.

 

OK, I hope it will help you! Code delivery:

Download Code

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.