如何在 PhoneApplicationService.Current.State 中保存和检索列表? [英] How to save and retrieve lists in PhoneApplicationService.Current.State?

查看:28
本文介绍了如何在 PhoneApplicationService.Current.State 中保存和检索列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 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>()
    {
        // 
    };

结果的值是从 Internet 获取的,当应用程序切换时,此数据需要保存在独立存储中以进行多任务处理.如何保存此列表并再次检索它?

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?

推荐答案

如果问题确实是关于如何保存数据,那么你就去做

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

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

并再次检索你做

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

这篇关于如何在 PhoneApplicationService.Current.State 中保存和检索列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆