Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
Windows Phone Zone is brought to you in partnership with:

I have worked for several companies as a senior software developer and lead developer. I bave enterprise level programming experience on Web, Silverlight, and Windows Phone 7. I am a MVP in Device Application Development since 2011. Jevgeni is a DZone MVB and is not an employee of DZone and has posted 40 posts at DZone. You can read more from them at their website. View Full User Profile

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

08.06.2011
Email
Views: 2857
  • submit to reddit
It's so easy to get up and running with your first Windows Phone app!  Just pick up the FREE toolkit from Microsoft, register at AppHub, and start checking out some fundamental tutorials.

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.

 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.

References
Published at DZone with permission of Jevgeni Tšaikin, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

The Windows Phone Microzone, which is supported by Microsoft, is your one-stop-shop for news, tutorials, perspectives, and research on the mobile platform that is making waves in smartphone ecosystem.

Comments

Gagan Peter replied on Sat, 2012/04/14 - 5:01am

You also, can emulate the functionality of the Session or cache variable in web development, This give you the option to have many variables saved and access to them with a key. One advantage of this is the posibility of centralize the store of variable. I prefer this, because is a reusable method in many developments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.