Session 4.5
Topics
The Story So Far 3
Multi-page applications 4
Adding another page 5
Pages and projects 6
Page Navigation private void page2button_click(object sender, RoutedEventArgs e) { NavigationService.Navigate( new Uri("/CustomerDetailPage.xaml", UriKind.RelativeOrAbsolute)); } 7
The UriKind private void page2button_click(object sender, RoutedEventArgs e) { NavigationService.Navigate( new Uri("/CustomerDetailPage.xaml", UriKind.RelativeOrAbsolute)); } 8
Missing page exceptions 9
Using the Back button 10
Overriding the Back button 11
Disabling the Back Button private void PhoneApplicationPage_BackKeyPress( object sender, System.ComponentModel.CancelEventArgs e) { e.cancel = true; } 12
Using a MessageBox private void PhoneApplicationPage_BackKeyPress( object sender, System.ComponentModel.CancelEventArgs e) { if (MessageBox.Show("Do you really want to exit?", "Page Exit", MessageBoxButton.OKCancel)!= MessageBoxResult.OK) { e.cancel = true; } } 13
Passing data between pages 14
Assembling a data uri // Get the selected customer from the list Customer selectedcustomer = customerlist.selecteditem as Customer; // Build a navigation string containing the information NavigationService.Navigate( new Uri("/CustomerDetailPage.xaml?" + "name=" + selectedcustomer.name + "&" + "address=" + selectedcustomer.address, UriKind.Relative)); 15
Page navigated events 16
Loading data from the uri protected override void OnNavigatedTo (System.Windows.Navigation.NavigationEventArgs e) { string name, address; if (NavigationContext.QueryString.TryGetValue("name", out name)) nametextblock.text = name; } 17
Demo 2: Passing Data 18
Sharing objects between pages 19
The App.xaml page 20
The App class public partial class App : Application { // To be used from all pages in the application public Customer ActiveCustomer; } 21
Getting a reference to the App protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e) { base.onnavigatedto(e); // Get the parent App containing the active customer App thisapp = Application.Current as App; // Set the data context for the Grid to the selected // customer customerdisplaygrid.datacontext = thisapp.activecustomer; } 22
Getting a reference to the App protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e) { base.onnavigatedto(e); // Get the parent App containing the active customer App thisapp = Application.Current as App; // Set the data context for the Grid to the selected // customer customerdisplaygrid.datacontext = thisapp.activecustomer; } 23
Getting a reference to the App protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e) { base.onnavigatedto(e); // Get the parent App containing the active customer App thisapp = Application.Current as App; // Set the data context for the Grid to the selected // customer customerdisplaygrid.datacontext = thisapp.activecustomer; } 24
Getting a reference to the App protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e) { base.onnavigatedto(e); // Get the parent App containing the active customer App thisapp = Application.Current as App; // Set the data context for the Grid to the selected // customer customerdisplaygrid.datacontext = thisapp.activecustomer; } 25
Setting the Edit Data Context protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e) { base.onnavigatedto(e); // Get the parent App containing the active customer App thisapp = Application.Current as App; // Set the data context for the Grid to the selected // customer customerdisplaygrid.datacontext = thisapp.activecustomer; } 26
Demo 3: Shared Data 27
Review 28