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

Passing values between Windows Phone 7 pages: Current context of Application

DZone's Guide to

Passing values between Windows Phone 7 pages: Current context of Application

· 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 approach for passing parameters and values between two Windows Phone 7 pages is in using a current context of application (i.e App.xaml.cs file). As it is for Page Instance approach and for QueryString approach this one also has several advantages and disadvantages.

Passing values between Windows Phone 7 pages: Current context of 
Application image by eugenedotnet aka jevgeni tsaikin

Implementation

Probably it is one of the easiest approaches for sharing values between several different Windows Phone 7 pages. Actually, not only pages, but you can share properties between all parts of your application. For this purpose you can use an object of App class (App.xaml.cs).

I have added a custom property to App.xaml.cs:

public string Parameter { get; set; }
 

To store a value for this property you can use the following line of code from any part of your application:

var a = App.Current as App;
a.Parameter = "something";
 

Same code can be used to read a value of that property in another part of your application:

var a = App.Current as App;
myTextBlock.Text = a.Parameter;
 

Advantages

First of all, the parameter or property is shared not only between two pages, but it is accessible from any part of your application. Secondly, it is easier to store and restore the value during the Tombstoning events if it is a part of App.xaml.cs class, because by default Tombstoning events are handled by the same class.

Disadvantages

Actually, this approach doesn’t have many downsides or maybe I was not able to find them. One problem, which I had using this way of passing values, is that it is sometimes hard to define the right time to remove a value from a property. Always keep in mind the performance and memory usage considerations while developing for a mobile device.

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