Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
161 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...