1 / 20

Charles Petzold www.charlespetzold.com

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

sondra
Télécharger la présentation

Charles Petzold www.charlespetzold.com

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Navigation Charles Petzold www.charlespetzold.com

  2. Agenda • Navigation framework • Passing data between pages • Navigation and state retention • Self-referential pages

  3. 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

  4. Navigation Framework Classes

  5. 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

  6. Adding a New Page to an App • Use Visual Studio's Add New Item command • Select one of the phone-page templates

  7. 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)

  8. 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();

  9. 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();

  10. 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 }

  11. Navigation Applications

  12. 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

  13. 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"]; }

  14. NavigationContext

  15. Navigation and State • PhoneApplicationFrame.GoBack activates an existing instance of the previous page • Same is true of phone's Back button Navigate() GoBack()

  16. Navigation and State, Cont. • NavigationService.Navigate creates a new instance of the page being navigated to Navigate() Navigate()

  17. 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 }

  18. Navigation and Tombstoning

  19. Self-Referential Pages • Pages that navigate to themselves • Can be used to avoid complex tombstoning logic if query strings wholly capture page state

  20. Questions? Charles Petzold www.charlespetzold.com

More Related