Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Another Way of Passing Values Between Windows Phone 7 Pages

DZone's Guide to

Another Way of Passing Values Between Windows Phone 7 Pages

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Another appropriate way for passing data from one page to another during the navigation is in setting up destination page properties (or variables) within OnNavigatedFrom method of source page.

passing data or parameters from one page to another using onnavigatedfrom method and destination page instance by eugenedotnet


OnNavigatedFrom method is called just before a page is no longer the active page in a frame. For example this method is called if you invoke NavigationService.Navigate method from a source page. Due to the fact that OnNavigatedFrom method accepts NavigationEventArgs as input parameter you have access to the destination page stored in Content property.

Sample

I have created two pages: Page1.xaml and Page2.xaml. Inside Page2 I have added a string property named Parameter. Inside Page1 (codebehind) I have overridden method OnNavigatedFrom for setting up Page2′s Parameter property.

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    Page2 page2Instance = e.Content as Page2;
    if (page2Instance != null)
    {
        page2Instance.Parameter = "value";
    }
}


Advantages

Probably one of advantages is that you don’t have to store values in static context, but simply pass them from one page to another. That approach is great for a wizard when you have several pages in a row and you need to store only the final result after wizard is completed.

Disadvantages

This approach cannot be applied to applications that can lose sensitive information during the suspend mode (Tombstoning), because when system restores your application from suspend mode there is no way to call OnNavigatedFrom method to set destination page properties.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Jevgeni Tšaikin. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}