Initial Windows Phone development experience (3)-parameter transfer

Source: Internet
Author: User

In the previous section, we learned how to process the page navigation of windowsphone and realized data transmission between two pages. In actual development, we also need to transmit data for two pages. After reading the official documents and online data collection, there are four methods to transmit the summary parameters:

1. Use the querystring method of navigationcontext;

2. Set global variables through the app class of the Program (this method can pass objects );

3. Use the content attribute settings of the navigationeventargs event class;

4. Use the state attribute of the phoneapplicationservice class.

 

The querystring method of navigationcontext is described in the previous section. The following three types

 

(1) set global variables through the app class of the Program (this method can pass objects)

Create a "model" folder under the project directory, and then create a class: person class in this folder.

public class Person    {        public String Name { get;set; }        public int Age { get; set; }     }

Then add the code in the app. XAML. CS file.

Public partial class app: Application {/// <summary> /// provides easy access to the root framework of the Telephone Application. /// </Summary> /// <returns> root framework of the Telephone Application. </Returns> Public phoneapplicationframe rootframe {Get; private set;} public static personperson {Get; Set ;}........}

In the new apphome. XAML. CS file, add a click event for the button on the page.

private voidbutton1_Click(object sender, RoutedEventArgs e)        {            App.person= new Model.Person            {                Name=txt_Name.Text,                Age=Convert.ToInt32(txt_Age.Text)                       };             Uriuri = new Uri("/AppData.xaml",UriKind.Relative);            NavigationService.Navigate(uri);        } 

Then, the passed value is accepted in the loaded event of the appdata. XAML file:

private voidLayoutRoot_Loaded(object sender, RoutedEventArgs e)        {            if(App.person!=null)            {                tb_Name.Text = App.person.Name;                tb_Age.Text = App.person.Age.ToString();            }        }

Final effect:

(1) Use the content attribute settings of the navigationeventargs event class

 

In the contentpage1.xaml. CS file:

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)        {            vartargetPage = e.Content as ContentPage2;            if(targetPage != null)            {                targetPage.StrContent=textBox1.Text;            }        }         privatevoid button1_Click(objectsender, RoutedEventArgs e)        {            NavigationService.Navigate(new Uri("/ContentPage2.xaml",UriKind.Relative));        }

Contentpage2.xaml. CS:

 public String StrContent { get;set; }         protectedoverride voidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgse)        {            if(StrContent != null)            {                this.tb_ContentValue.Text= StrContent;            }        }

Implementation results:

 

(2) using the state attribute of the phoneapplicationservice class:

Statepage1.xaml. CS file:

protected override voidOnNavigatedFrom(System.Windows.Navigation.NavigationEventArgse)        {            PhoneApplicationServicephoneService = PhoneApplicationService.Current;             phoneService.State["param1"] = this.textBox1.Text;        }         privatevoid button1_Click(objectsender, RoutedEventArgs e)        {            NavigationService.Navigate(new Uri("/StatePage2.xaml",UriKind.Relative));        }

Statepage2.xaml. CS file:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)        {            if(PhoneApplicationService.Current.State.ContainsKey("param1"))            {                this.tb_ParamValue.Text= PhoneApplicationService.Current.State["param1"] asstring;                           }        }         privatevoid button1_Click(objectsender, RoutedEventArgs e)        {            if(NavigationService.CanGoBack)            {                NavigationService.GoBack();            }        }

Effect:

If you need to reprint reference please indicate the source: http://blog.csdn.net/jiahui524

Source code: http://download.csdn.net/detail/jiahui524/4316106

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.