How to save and retrieve lists in PhoneApplication

2019-08-18 08:54发布

问题:

I need to store and retrieve lists in PhoneApplicationService.Current.State[] but this is not a list of strings or integers:

    public class searchResults
    {
        public string title { get; set; }
        public string description { get; set; }
    }

    public List<searchResults> resultData = new List<searchResults>()
    {
        // 
    };

The values of the result are fetched from internet and when the application is switched this data needs to be saved in isolated storage for multitasking. How do I save this list and retrieve it again?

回答1:

If the question really is about how to save the data then you just do

    PhoneApplicationService.Current.State["SearchResultList"] = resultData;

and to retrieve again you do

    List<searchResults> loadedResultData = (List<searchResults>)PhoneApplicationService.Current.State["SearchResultList"];

Here is a complete working sample:

    // your list for results
    List<searchResults> resultData = new List<searchResults>();
    // add some example data to save
    resultData.Add(new searchResults() { description = "A description", title = "A title" });
    resultData.Add(new searchResults() { description = "Another description", title = "Another title" });
    // save list of search results to app state
    PhoneApplicationService.Current.State["SearchResultList"] = resultData;

    // --------------------->
    // your app could now be tombstoned
    // <---------------------

    // load from app state
    List<searchResults> loadedResultData = (List<searchResults>)PhoneApplicationService.Current.State["SearchResultList"];

    // check if loading from app state succeeded
    foreach (searchResults result in loadedResultData)
    {
        System.Diagnostics.Debug.WriteLine(result.title);
    }

(This might stop working when your data structure gets more complex or contains certain types.)



回答2:

Sounds like you just want to employ standard serialisation for your list object, see here in the MSDN docs http://msdn.microsoft.com/en-us/library/ms973893.aspx

Or also XML serialisation if you want something that can be edited outside of the application (you can also use the Isolated Storage exploter to grab the file off and edit later) http://msdn.microsoft.com/en-us/library/182eeyhh(v=vs.71).aspx

Alternatively i would also suggest trying out the Tombstone Helper project by Matt Lacey which can simplify this for you greatly http://tombstonehelper.codeplex.com/



回答3:

The answer by Heinrich already summarizes the main idea here - you can use the PhoneApplicationService.State with Lists like with any objects. Check out the MSDN docs on preserving application state: How to: Preserve and Restore Application State for Windows Phone. There's one important point to notice there:

Any data that you store in the State dictionary must be serializable, either directly or by using data contracts.

Directly here means that the classes are marked as [Serializable]. Regarding your List<searchResults>, it is serializable if searchResults is serializable. To do this, either searchResults and all types referenced by it must be marked with the [Serializable] OR it must be a suitable Data Contract, see Using Data Contracts and Serializable Types. In short, make sure the class is declared as public and that it has a public, parameterless constructor.