Passing a complex object to a page while navigating in a WP7 Silverlight application

This is an extremely complex problem, and there is no simple solution here. There is no magic API that would just work for any app to solve this problem.

The core problem with passing navigation data is Tombstoning. The only piece of data that is tombstoned by default is the Navigation URI. so if you're using a QueryString parameter, it'll get picked up automatically by tombstoning and your code. Any time you'll manually pass a instance of an object though, you'll have to manually do tombstoning for that instance yourself.

So, if you navigate to "/CowDetails.xaml?ID=1" your page will probably have perfect tombstoning just by picking up on the ID Querystring Parameter. However, if you somehow provide CowDetails page with a "new Cow() { ID = 1}" you'll have to make sure to tombstone and zombificate this value yourself.

Also, there's the issue of timing. While calling NavigationService.Navigate, the page you're navigating too doesn't have an actual instance yet. So even though you're navigating to FooPage and have the FooData ready, there's no way to immediately connect FooPage to FooData. You'll have to wait until the PhoneApplicationFrame.Navigated event has fired in order to provide FooPage with FooData.

The way I normally deal with this is problem:

  1. Have a BasePage with an Object type Data property
  2. Have a NavigationHelper get the page URI and Data: NavigationHelper.Navigate("foo.xaml", fooData)
  3. Have NavigationHelper register to PhoneApplicationFrame.Navigated event and if it's "foo.xaml" set the BasePage.Data to FooData.
  4. Have BasePage use JSON.Net to tombstone and zombificate BasePage.Data.
  5. On BasePage, I've got a OnDataSet virtual method that is invoked once the Data property is populated either by Zombification or Navigation. In this method, everything that has to do with business data happens.

App.xaml.cs -> App class, add a field/property there. To access it, if it is static use:

App.MyComplexObject

Or if is not staic

(App.Current as App).MyComplexObject;

There's a very simple solution to tackle this problem. Consider the following example A windows phone app has following two pages, Page1.xaml and Page2.xaml Lets say from Page1.xaml we are navigating to Page2.xaml. The navigation cycle starts, when you call NavigationService.Navigate method

  1. First OnNavigatingFrom Event of Page1 fires
  2. Then Constructor of Page2 fires
  3. Then OnNavigatedFrom event of Page1 fires with the reference of the created page in its EventArgs (e.Content has the created instance of Page2)
  4. Finally OnNavigatedTo Event of Page2 fires

So we are getting the other page's reference in the page where the navigation starts.

public class PhoneApplicationBasePage : PhoneApplicationPage
{

private object navParam = null;
protected object Parameter{get;private set;}
//use this function to start the navigation and send the object that you want to pass 
//to the next page
protected void Navigate(string url, object paramter = null)
 {    
   navParam = paramter;
   this.NavigationService.Navigate(new Uri(url, UriKind.Relative));
 }

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
//e.Content has the reference of the next created page
if (e.Content is PhoneApplicationBasePage )
  {
   PhoneApplicationBasePage  page = e.Content as PhoneApplicationBasePage;
   if (page != null)
   { page.SendParameter(navParam); navParam=null;}
  }
}
private void SendParameter(object param)
{
 if (this.Parameter == null)
  {
   this.Parameter = param;
   this.OnParameterReceived();
  }
}
protected virtual void OnParameterReceived()
{
//Override this method in you page. and access the **Parameter** property that
// has the object sent from previous page
}
}

So in our Page1.xaml.cs we simply call Navigate("/Page2.xaml",myComplexObject). And in your Page2.xaml.cs we will override OnParameterReceived method

 protected override void OnParameterReceived()
{
var myComplexObjext = this.Parameter;
}

And it is also possible to handle tombstone problems with little more tweaks in PhoneApplicationBasePage


Debatable solution, if anything, make it temporary.

Create this under the namespace of your application for whichever page necessary.

public static class VarsForPages {

    // Be sure to include public static.
    public static SomeClass SomeClassObject;
    public static List<string> SomeList = new List<string>();
    public static string SomeData = "SomeValue";

}

// The syntax for referencing the data
   VarsForPages.SomeClassObject; 
   VarsForPages.SomeList; 
   VarsForPages.SomeData;

Now you can reference SomeClassObject, SomeList, SomeData anywhere in the application.

Note: Like any global data, be weary of the many accesses & modifications which may be done to the global data. I say this, because I once had a list increase in size, but one of my pages in the application relied on the size of the list to be of some value, and this caused a bug. Don't forget, the data is global.