将字符串保存到隔离存储中,并在下载新字符串后刷新旧存储

时间:2013-11-16 23:44:29

标签: c# windows-phone-8 isolatedstorage

我已经使用webclient下载了一些json,并在反序列化后将其保存到隔离存储中作为字符串。所以它们现在已经保存了,我会通过应用程序开始显示它们,因为webclient会下载它们,所以在下载之后它们仍然存在,我怎么能用新下载的数据刷新它们而不是中断它们以免眨眼。 / p>

我有这段代码:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("temp"))
{
    settings.Add("temp", tempTextBlock.Text.ToString());
}
else
{
    settings["temp"] = tempTextBlock.Text.ToString();
}

将temp保存到iso。我在“OnNavigatedTo”上使用它们,所以当应用程序启动时我会看到它们,但即使在后台下载数据后它们仍然存在。我该怎么做才能刷新我用下载的新数据保存的字符串?是否有一些方法来比较数据,然后只显示较新的数据?

这是代码:

    public MainViewModel()
    {
        if (IsInDesignMode)
        {
            DayList = new ObservableCollection<List>();
            DayList.Add(new List { Time = DateTime.Now, weather = new System.Collections.Generic.List<Weather> { new Weather { icon = "01d", description = "sunćano" } }, main = new Main {temp = 8 } });
            DayList.Add(new List { Time = DateTime.Now.AddHours(1), weather = new System.Collections.Generic.List<Weather> { new Weather { icon = "01d", description = "sunćano" } }, main = new Main { temp = 10 } });
            DayList.Add(new List { Time = DateTime.Now.AddHours(2), weather = new System.Collections.Generic.List<Weather> { new Weather { icon = "01d", description = "sunćano" } }, main = new Main { temp = 6 } });
            CurrentDay = DayList[0];
        }
        else
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("temp"))
            {

在这里,我想检查保存的字符串是否更新或者数字是否不同,然后现在将新的字符串下载。问题是此代码在下载后仍然存在,并且稍后在另一次下载时不会更改。因为没有比较:(                 }                 其他                 {                 }

        }
    }

我在下载完成时使用它来显示MainPage上的数据...

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            RootObject json = JsonConvert.DeserializeObject<RootObject>(e.Result);
            DayList = new ObservableCollection<List>(json.list);
            CurrentDay = DayList[0];
        }
        else
        {
            MessageBox.Show("error");
        }
    }

1 个答案:

答案 0 :(得分:0)

我发现在隔离设置中,如果您使用静态字符串,那么隔离设置将无法正常运行,因为它是在应用程序启动时创建的,因此如果您之后更改它只是存储,但在再次启动之前从未使用过,

隔离存储用于保护数据不是为了传递或存储目的