Wednesday, October 31, 2012

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

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?

Answers & Comments...

Answer: 1

If there is only one instance of your complex object simply pass a flag to Somepage.xaml to indicate it needs to access the object, and add methods to let the Somepage class access the object.

If you have list of these objects, and if they can be uniquely identified, then pass the identifier to Somepage.xaml and add a Find( myComplexObjectID ) method to the class holding these objects.

If they cannot be uniquely identified you'll have to do serialization of some sort. Either write the object to IsolatedStorage or convert it to a string (could be an XML string if you want to use XmlSerializer) and pass it as a query string. Also, if you do choose the query string option, don't forget to escape it using Uri.EscapeDataString(). You don't need to unescape it while reading, NavigationContext automatically does that for you.

by : Praetorianhttp://stackoverflow.com/users/241631

Answer: 2

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; 
by : lukashttp://stackoverflow.com/users/336186

Answer: 3

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.
by : JustinAngelhttp://stackoverflow.com/users/81687

Answer: 4

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.

by : Ryanhttp://stackoverflow.com/users/1061644

Answer: 5

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

by : vjsrinathhttp://stackoverflow.com/users/1069018




2 comments:

Anonymous said...

Excellent site you have got here.. It's difficult to find high quality writing like yours nowadays. I really appreciate individuals like you! Take care!!

Here is my site :: click here

Anonymous said...

rosacea laser treatment Walker Springs

My blog; laser treatment rosacea Osbornsville

Post a Comment

Send us your comment related to the topic mentioned on the blog