charles petzold navigation
TRANSCRIPT
![Page 1: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/1.jpg)
Charles Petzoldwww.charlespetzold.com
Navigation
![Page 2: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/2.jpg)
Agenda
• Navigation framework• Passing data between pages• Navigation and state retention• Self-referential pages
![Page 3: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/3.jpg)
• 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
![Page 4: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/4.jpg)
Navigation Framework Classes
Class Description
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
![Page 5: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/5.jpg)
Phone Application Structure
App Derives from System.Windows.Application
RootFrame
MainPage
Other Pages
Derives from Microsoft.Phone.-Controls.PhoneApplicationFrameDerives from Microsoft.Phone.-Controls.PhoneApplicationPage
![Page 6: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/6.jpg)
• Use Visual Studio's Add New Item command• Select one of the phone-page templates
Adding a New Page to an App
![Page 7: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/7.jpg)
• 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)
![Page 8: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/8.jpg)
• 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 pageif ((Application.Current as App).RootFrame.CanGoBack) (Application.Current as App).RootFrame.GoBack();
![Page 9: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/9.jpg)
• 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 itif ((Application.Current as App).RootFrame.CanGoForward) (Application.Current as App).RootFrame.GoForward();
![Page 10: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/10.jpg)
• 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}
![Page 11: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/11.jpg)
demoNavigation Applications
![Page 12: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/12.jpg)
• 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
![Page 13: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/13.jpg)
Using NavigationContext
// Page 1NavigationService.Navigate(new Uri("/Page2.xaml?ID=foo", UriKind.Relative));
// Page 2protected override void OnNavigatedTo(NavigationEventArgs e){ if (NavigationContext.QueryString.ContainsKey("ID")) string id = NavigationContext.QueryString["ID"];}
![Page 14: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/14.jpg)
demoNavigationContext
![Page 15: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/15.jpg)
• PhoneApplicationFrame.GoBack activates an existing instance of the previous page– Same is true of phone's Back button
Navigation and State
Navigate() GoBack()
![Page 16: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/16.jpg)
• NavigationService.Navigate creates a new instance of the page being navigated to
Navigation and State, Cont.
Navigate() Navigate()
![Page 17: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/17.jpg)
• 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}
![Page 18: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/18.jpg)
demoNavigation and Tombstoning
![Page 19: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/19.jpg)
• Pages that navigate to themselves• Can be used to avoid complex tombstoning
logic if query strings wholly capture page state
Self-Referential Pages
![Page 20: Charles Petzold Navigation](https://reader036.vdocuments.mx/reader036/viewer/2022062321/56649db15503460f94aa05a8/html5/thumbnails/20.jpg)
Charles Petzoldwww.charlespetzold.com
Questions?