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?
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:
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With