Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have been using the NavigationService's Navigate method to navigate to other pages in my WP7 Silverlight app:

NavigationService.Navigate(new Uri("/Somepage.xaml?val=dreas", UriKind.Relative));

From Somepage.xaml, I then retrieve the query string parameters as follows:

string val;
NavigationContext.QueryString.TryGetValue("val", out val);

I now need a way to pass a complex object using a similar manner. How can I do this without having to serialize the object every time I need to pass it to a new page?

like image 539
Andreas Grech Avatar asked Jan 15 '11 19:01

Andreas Grech


2 Answers

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.
like image 140
JustinAngel Avatar answered Sep 21 '22 13:09

JustinAngel


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;
like image 24
Lukasz Madon Avatar answered Sep 21 '22 13:09

Lukasz Madon