Charles Petzold Navigation

Preview:

Citation preview

Charles Petzoldwww.charlespetzold.com

Navigation

Agenda

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

• 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

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

Phone Application Structure

App Derives from System.Windows.Application

RootFrame

MainPage

Other Pages

Derives from Microsoft.Phone.-Controls.PhoneApplicationFrameDerives from Microsoft.Phone.-Controls.PhoneApplicationPage

• Use Visual Studio's Add New Item command• Select one of the phone-page templates

Adding a New Page to an App

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

• 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();

• 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();

• 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}

demoNavigation Applications

• 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

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

demoNavigationContext

• PhoneApplicationFrame.GoBack activates an existing instance of the previous page– Same is true of phone's Back button

Navigation and State

Navigate() GoBack()

• NavigationService.Navigate creates a new instance of the page being navigated to

Navigation and State, Cont.

Navigate() Navigate()

• 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}

demoNavigation and Tombstoning

• Pages that navigate to themselves• Can be used to avoid complex tombstoning

logic if query strings wholly capture page state

Self-Referential Pages

Charles Petzoldwww.charlespetzold.com

Questions?

Recommended