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

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.

Topics:

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}