Value Transfer and object transfer on Windows Phone 7 page

Source: Internet
Author: User
1. how to pass Parameters on winodws Phone 7: 1. Use the navigateuri attribute of hyperlinkbutton to add the passed parameters directly to the navigateuri attribute. For example
<Hyperlinkbutton content = "Music" Height = "30" horizontalalignment = "Left" margin = "68,65, 200 "name =" musicshyperlinkbutton "verticalalignment =" TOP "width =" "navigateuri =" musics? Songname = love you for 10 thousand years "/>

Explanation: navigateuri = "musics? Songname = 10 thousand 10 thousand "indicates to navigate to a page named musics and pass the songname parameter to the navigation page. The value of songname is equal to that of years (if you want to pass the parameter, need to use? Symbol indicates the parameters to be passed ).

The data is uploaded to another page. The following figure shows how to obtain the data on the navigation page. You can call the querystring attribute of navigationcontext to obtain the data. The Code is as follows:

txt_SongName.Text = NavigationContext.QueryString["SongName"];

The code above indicates to get the parameter value of the variable named songname and assign it to a textbox. In this way, data is transmitted between pages. This is method 1.

 

2. You can use the button control to navigate and call navigationservice to transmit parameters. For example:
Navigationservice. navigate (New uri ("musics? Songname = love you for 10 thousand years & songauthor = Andy Lau ", urikind. Relative ));

Code explanation: the red part indicates two variables passed, songname and songauthor. Values are assigned respectively. urikind. Relative indicates that the path on the navigation page is relative.

After passing a parameter, you can use method 1 to obtain the value passed by the parameter. (If multiple parameters need to be passed, use & connect each parameter together ).

3. modify the code in <application. Resources>... </application. Resources> to implement alias transfer data.

 

<nav:UriMapper x:Key="UriMapper">            <nav:UriMapping Uri="Musics" MappedUri="/Views/MusicsPage.xaml"/></nav:UriMapper>

 

The above Code does not need to pass data. If you want to pass data, you can modify it as follows:

<nav:UriMapper x:Key="UriMapper">  <nav:UriMapping Uri="Musics/{song}" MappedUri="/Views/MusicsPage.xaml?SongName={song}"/></nav:UriMapper>

Note that the content of {} in musics/{SONG} must be exactly the same as that in {} In songname = {SONG.

The above Code declares a variable named songname, and the content of {} can be named at will.

If you want to pass multiple parameters, you can modify them as follows:

<nav:UriMapping Uri="Musics/{song}&amp;{author}" MappedUri="/Views/MusicsPage.xaml?SongName={song}&amp;SongAuthor={author}"/>

If there are more parameters, add variables in sequence.

The following is the assignment process.

<Hyperlinkbutton content = "Music" Height = "30" horizontalalignment = "Left" margin = "68,65, 200 "name =" musicshyperlinkbutton "verticalalignment =" TOP "width =" 10 thousand "navigateuri =" musics/love you for years "/>

Code Description: navigateuri = "musics/Song". After the parameter value is passed, the value is equivalent to: songname = 10 thousand years for loving you. The value is the same as before.

2. How to pass objects in Windows Phone 7.

Navigationservice cannot pass objects. It can only pass string-type data.

Therefore, the above numeric value transfer method cannot be used to pass objects.

So what is the implementation method? You can use static attributes of the app class or the singleton class.

The following describes how to use the static attributes of the app to pass objects.

First, add a class, musicinformation, which contains two attributes:

 

Public class musicinformation {// automatically add get, Set Method Public String songname {Get; Set ;}// song name Public String songauthor {Get; Set ;}// song author}

 

After adding the class, we need to declare the variables of this class (musicinformation) in APP. XAML. CS:

Public static musicinformation musciinfor {Get; set;} // defines a variable of the musicinformation type.

You can call this static property after it is declared and assign values to songname and songauthor. The assignment process can be completed in the response event on the navigation page, for example, in button_click.

Private void btn_music_click (Object sender, routedeventargs e) {app. musciinfor = new musicinformation {songname = ".mp3 .mp3", songauthor = "Liu Dehua"}; // C # new writing method. You can assign navigationservice to the variable directly without using the constructor. navigate (New uri ("musics", urikind. relative); // navigate to the page with the alias musics. }

After navigation to the page, you can read the transmitted object data in the loaded event of the page alias musics.

Private void phoneapplicationpage_loaded (Object sender, routedeventargs e) {If (App. musciinfor! = NULL) // determines whether the transmitted object data is null. If not, assign {txt_authory.text = app. musciinfor. songauthor; txt_songname.text = app. musciinfor. songname ;}}

In this way, the object data is transmitted.

 

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.