如何计算隔离存储设置的行?

时间:2012-09-04 05:12:43

标签: windows-phone-7 isolatedstorage

我在隔离存储设置中存储了多个数据,但当时的反向数据只显示一个数据

我使用for循环,但它没有给出完美的输出

c#代码是:


    for (int i = 0; i <= IsolatedStorageSettings.ApplicationSettings.Count; i++)
    {
      cityname = IsolatedStorageSettings.ApplicationSettings["CityDetail"] as string;
      ads.Add(new Transaction(cityname));
    }

保存隔离存储设置的代码


 if (!setting.Contains("CityDetail"))
 {
   setting.Add("CityDetail", cityname);
 }
 else
 {
   setting["CityDetail"] = cityname; 
 }
 setting.Save();

1 个答案:

答案 0 :(得分:1)

Isolated Storage使用键值对来保存数据 例如,如果你这样做:


 if (!setting.Contains("CityDetail"))
 {
   setting.Add("CityDetail", "abc");
 }
 else
 {
   setting["CityDetail"] = "abc"; 
 }
 setting.Save();

因此,执行此操作后,键 - CityDetail abc

之后如果你这样做:


 if (!setting.Contains("CityDetail"))
 {
   setting.Add("CityDetail", "def");
 }
 else
 {
   setting["CityDetail"] = "def"; 
 }
 setting.Save();

然后,键 CityDetail 将被替换,新的 def
本质是“在键值存储方法中,一个键只能有一个值。”

要针对单个密钥保存多个值,您可以执行以下操作:


void addCityName(string cityName)
{
    List<string> existingList = getCityNames();
        if(existingList==null)
        {
             existingList = new List<string>();
             existingList.Add(cityName);
        }
        string json = JsonConvert.SerializeObject(exitingList);
        if(!IsolatedStorageSettings.ApplicationSettings.Contains("CityDetails"))
    {
        IsolatedStorageSettings.ApplicationSettings.Add("CityDetails",json);    
    }
    else 
    {
        IsolatedStorageSettings.ApplicationSettings["CityDetails"] = json;
    }
}

List<string> getCityNames()
{
    string json = IsolatedStorageSettings.ApplicationSettings["CityDetails"] as string;
    if(json == null)
        return null;
    JArray arr = JArray.parse(json);
        List<string> list = new List();
    for(int i=0; i < arr.length; i++)
    {
        list.add(arr[i].toString());
    }
    return list;
}

为此,您需要Json.Net库。