9

I have the exact same question as Passing data from page to page, but only for Windows Phone 8.1 (opposed to Windows Phone 7). Here is the question:

I'm looking for the best practice on how to pass data from page to page.

In Page A I have a button that fires off Page B. On Page B I have 6 textboxes that allow the user to enter information. When the user is done, the click on a button that brings them back to Page A.

I want to pass that data back to Page A.

I've seen suggestions to:

build XML documents and save to Isolated Storage use the App class to store information in properties pass it like a query string I'm looking for the Best practice. Is there one that Microsoft recommends or one that is generally accepted as the best way?

Thanks

2 Answers 2

16

In WP8.1 Runtime - for Silverlight, the methods used in WP8.0 still should work - you have couple of choces:

  • the first and probably the easiest way is to use Navigate with parameter - you don't have to convert it to string if it's a serializable type:

    // let's assume that you have a simple class:
    public class PassedData
    {
       public string Name { get; set; }
       public int Value { get; set; }
    }
    
    // then you navigate like this:
    Frame.Navigate(typeof(Page1), new PassedData { Name = "my name", Value = 10 });
    
    // and in target Page you retrive the information:
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        PassedData data = e.Parameter as PassedData;
    }
    
  • you can use some static objects to pass your data along the App

  • finally as you have mentioned you can save your data in: LocalSettings, LocalStorage, LocalCache - thought in this case you will also have to serialize it to XAML, Json or other.

Note that you will also have to handle app suspending/resuming - so it will be suitable to save your data when the app is being suspended and load when it's resumed. You should remember that OnNavigatedTo is not being called when the app is resuming.


The above was about normal navigation (forward). If you want to fill some data in previous Page, then you have a couple of options:

  • pass a handler to a previous Page, so you can access public variables/properties from current Page,
  • use a static variable/property - maybe a singleton
  • again use files/settings

Note that again the first two methods has a disadvantage that the app may crash after being suspended. Saving to files might be better here, thought needs some more work and proper handling.

4
  • if we want to pass more than one parameter then what approach is better? Commented Mar 9, 2015 at 7:03
  • @KinjanBhavsar It depends on types of your parameters, how important they are and other implementations.
    – Romasz
    Commented Mar 9, 2015 at 8:44
  • I want to pass an object of class and a string paramater which is from some other class. Commented Mar 9, 2015 at 10:48
  • @KinjanBhavsar It depends if your class consists of basic types. Give it a try and choose the most appropriate method for you. Don't forget about ap suspension and even termination.
    – Romasz
    Commented Mar 9, 2015 at 11:33
0

--First Create a class

public class PassedData
{
   public string email { get; set; }  
}

--then You can navigate within pages hanging the value

Frame.Navigate(typeof(Frame2), new PassedData { email = emailtxt.Text });

--then catch the value that passing in another page

var passedData = e.Parameter as PassedData;

emailtxt.Text = passedData.email;

Not the answer you're looking for? Browse other questions tagged or ask your own question.