Passing Page Arguments Using Query Strings

,

Query strings allow for key value pairs to be embedded in a URL and passed to a page. Just like HTML web applications, Windows Phone apps use query string parameters for inter-page communication.

The PhoneApplicationPage.NavigationContext property, which is initialized after the page is created, is used to retrieve the query string. Its QueryString property is an IDictionary of string key and value pairs. The following excerpt from the WebBrowserView.xaml.cs, in the downloadable sample code, demonstrates how to retrieve a query string value:

protected overide void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    string url;
    if (NavigationContext.QueryString.TryGetValue("url", out url)
        && url != null && url.ToLower() != "webbrowserview.xaml")
    {
        ViewModel.LoadPage(url);
    }
}

Navigation History Stack

The Windows Phone navigation infrastructure maintains a history of pages that have been loaded. Each time an app navigates to a different page, the current page’s OnNavigatedFrom method is called and the page is placed on the history stack (see Figure 3.6).

Image

FIGURE 3.6 Pages are placed on the history stack.

While on the history stack, the page remains in memory unless the application is tombstoned or closed. This means that subscribing to the PhoneApplicationService.Deactivated event provides the page with the opportunity to save its transient state. It is, however, preferable to use the page’s OnNavigatedFrom method for the saving of page state.

Using the Deactivate event to save page state runs the risk of slowing down deactivation when all pages on the history stack are saving their state simultaneously.


Best Practices

Use the OnNavigatedFrom and OnNavigatedTo methods of PhoneApplicationPage to save both transient and persistent state.



Note

Unlike Silverlight for the browser, in Windows Phone the page’s NavigationCacheMode property is not assignable, and it is set to Disabled by default. This means that internal caching of pages does not occur, and when navigating to a page that does not exist on the history stack, the page is always instantiated.


Restoration of transient state should occur with the page’s OnNavigatedTo method. The OnNavigatedTo method is called when the PhoneApplicationFrame navigates to the page. This is triggered by the following actions:

Image Navigation to a specified page URI occurs using one of various navigation methods such as the PhoneApplicationFrame.Navigate method.

Image The NavigationService.GoBack method is called.

Image The user presses the hardware Back button.

Image The page is the current page when the app moves from the tombstoned or dormant state to the running state.

Image The page is the app’s start page and the app is launched.


Note

When an application is activated, the frame navigates to the page that was active when the phone was dormant or tombstoned. The NavigationContext.QueryString is preserved, which means that there is no need to store this in the PhoneApplicationService.State dictionary.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.51.241