Download presentation
Presentation is loading. Please wait.
Published byMarjorie Fowler Modified over 9 years ago
1
Charles Petzold www.charlespetzold.com Navigation
2
Agenda Navigation framework Passing data between pages Navigation and state retention Self-referential pages
3
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 Navigation Framework
4
Navigation Framework Classes ClassDescription PhoneApplicationPage Represents pages in a phone application. Includes key virtual methods OnNavigatedFrom and OnNavigatedTo, and key properties NavigationService and NavigationContext PhoneApplicationFrame Serves as a container for pages and provides space for the system tray and application bar. Also provides methods for navigating backward like the phone's Back button NavigationService Provides methods for navigating to other pages NavigationContext Provides easy-to-use mechanism for retrieving data passed from one page to another via query strings
5
Phone Application Structure App Derives from System.Windows.Application RootFrame MainPage Other Pages Derives from Microsoft.Phone.- Controls.PhoneApplicationFrame Derives from Microsoft.Phone.- Controls.PhoneApplicationPage
6
Use Visual Studio's Add New Item command Select one of the phone-page templates Adding a New Page to an App
7
NavigationService.Navigate goes to another page NavigationService reference exposed through PhoneApplicationPage.NavigationService Navigating to Another Page NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative)); Include leading slash (required)
8
PhoneApplicationFrame.GoBack goes back –Throws exception if there is no back PhoneApplicationFrame.CanGoBack indicates whether it's safe to call GoBack Going Backward // Go to the previous page if ((Application.Current as App).RootFrame.CanGoBack) (Application.Current as App).RootFrame.GoBack();
9
PhoneApplicationFrame.GoForward always throws InvalidOperationException –Windows phone has back stack but not forward stack PhoneApplicationFrame.CanGoForward always returns false Going Forward // Don't even try it if ((Application.Current as App).RootFrame.CanGoForward) (Application.Current as App).RootFrame.GoForward();
10
OnNavigatedTo and OnNavigatedFrom methods are called when page is navigated to or from Use OnNavigatedTo to perform initializations required each time page is displayed PhoneApplicationPage Overrides public MainPage() { // Not guaranteed to be called } protected override void OnNavigatedTo(NavigationEventArgs e) { // Guaranteed to be called }
11
demo Navigation Applications
12
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 Passing Data Between Pages
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
demo NavigationContext
15
PhoneApplicationFrame.GoBack activates an existing instance of the previous page –Same is true of phone's Back button Navigation and State Navigate()GoBack()
16
NavigationService.Navigate creates a new instance of the page being navigated to Navigation and State, Cont. Navigate()
17
Tombstoning code adds state retention –Retains state between activation events –Retains state between navigation events Use application state, not page state, for latter Navigation and Tombstoning 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
demo Navigation and Tombstoning
19
Pages that navigate to themselves Can be used to avoid complex tombstoning logic if query strings wholly capture page state Self-Referential Pages
20
Charles Petzold www.charlespetzold.com Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.