Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...