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