200 likes | 329 Views
Navigation. Charles Petzold www.charlespetzold.com. Agenda. Navigation framework Passing data between pages Navigation and state retention Self-referential pages. Navigation Framework. Navigation framework elements Pages to host navigable content Frame to host the pages
E N D
Navigation Charles Petzold www.charlespetzold.com
Agenda • Navigation framework • Passing data between pages • Navigation and state retention • Self-referential pages
Navigation Framework • Navigation framework elements • Pages to host navigable content • Frame to host the pages • Navigation service for moving among pages • Navigation context for passing data between pages • Navigation framework benefits • Build multipage applications • Partition content into navigable chunks • Get Back-button support for free
Phone Application Structure App Derives from System.Windows.Application RootFrame Derives from Microsoft.Phone.-Controls.PhoneApplicationFrame MainPage Derives from Microsoft.Phone.-Controls.PhoneApplicationPage Other Pages
Adding a New Page to an App • Use Visual Studio's Add New Item command • Select one of the phone-page templates
Navigating to Another Page • NavigationService.Navigate goes to another page • NavigationService reference exposed through PhoneApplicationPage.NavigationService NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative)); Include leading slash (required)
Going Backward • PhoneApplicationFrame.GoBack goes back • Throws exception if there is no back • PhoneApplicationFrame.CanGoBack indicates whether it's safe to call GoBack // Go to the previous page if ((Application.Current as App).RootFrame.CanGoBack) (Application.Current as App).RootFrame.GoBack();
Going Forward • PhoneApplicationFrame.GoForward always throws InvalidOperationException • Windows phone has back stack but not forward stack • PhoneApplicationFrame.CanGoForward always returns false // Don't even try it if ((Application.Current as App).RootFrame.CanGoForward) (Application.Current as App).RootFrame.GoForward();
PhoneApplicationPage Overrides • OnNavigatedTo and OnNavigatedFrom methods are called when page is navigated to or from • Use OnNavigatedTo to perform initializations required each time page is displayed public MainPage() { // Not guaranteed to be called } protected override void OnNavigatedTo(NavigationEventArgs e) { // Guaranteed to be called }
Passing Data Between Pages • Use NavigationContext • Equivalent to using query strings in Web apps • Best for simple data that's small in volume • Use application variables • Public fields or properties declared in App class • Handles large amounts of data, simple or complex • Or use the application state • Accessed through PhoneApplicationService.State • Limit of ~1.5 MB of data and must be serializable
Using NavigationContext // Page 1 NavigationService.Navigate(new Uri("/Page2.xaml?ID=foo", UriKind.Relative)); // Page 2 protected override void OnNavigatedTo(NavigationEventArgs e) { if (NavigationContext.QueryString.ContainsKey("ID")) string id = NavigationContext.QueryString["ID"]; }
Navigation and State • PhoneApplicationFrame.GoBack activates an existing instance of the previous page • Same is true of phone's Back button Navigate() GoBack()
Navigation and State, Cont. • NavigationService.Navigate creates a new instance of the page being navigated to Navigate() Navigate()
Navigation and Tombstoning • Tombstoning code adds state retention • Retains state between activation events • Retains state between navigation events • Use application state, not page state, for latter protected override void OnNavigatedFrom(NavigationEventArgs e) { // TODO: Record page state in application state } protected override void OnNavigatedTo(NavigationEventArgs e) { // TODO: Restore page state from application state }
Self-Referential Pages • Pages that navigate to themselves • Can be used to avoid complex tombstoning logic if query strings wholly capture page state
Questions? Charles Petzold www.charlespetzold.com