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

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

windows phone 7 - How to save and retrieve lists in PhoneApplicationService.Current.State?

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?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...