我正在使用像缓存这样的localstorage来保存数据:
public void AddtoFavorite(Flight FavoriteFlight)
{
try
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Favorite.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(Flight));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, FavoriteFlight);
}
}
}
}
catch (Exception ex)
{
}
}
和此方法获取数据:
public FavoriteFlight GetFavorite()
{
FavoriteFlight result = new FavoriteFlight();
result.VisibleFavorite = false;
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Favorite.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(Flight));
Flight fav=(Flight)serializer.Deserialize(stream);
result.FlightFavorite = fav;
result.Date = result.FlightFavorite.ArrivalOrDepartDateTime.ToString("dd/MM/yyyy");
result.VisibleFavorite = true;
return result;
}
}
}
catch
{
return result;
}
}
我需要localstorage每24小时到期一次,以获取本地存储的价值,你怎么能这样做?
问候
答案 0 :(得分:1)
只需将SavedDate
字段添加到Flight
对象中,然后将其保存到隔离存储中,并在检索对象时检查它是否少于24小时。
public void AddtoFavorite(Flight FavoriteFlight)
{
try
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Favorite.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(Flight));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
FavoriteFlight.SavedDate = DateTime.Now;
serializer.Serialize(xmlWriter, FavoriteFlight);
}
}
}
}
catch (Exception ex)
{
}
}
public FavoriteFlight GetFavorite()
{
FavoriteFlight result = new FavoriteFlight();
result.VisibleFavorite = false;
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Favorite.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(Flight));
Flight fav=(Flight)serializer.Deserialize(stream);
if ((DateTime.Now - fav.SavedDate).TotalHours > 24)
{
// TODO: Refresh the value
}
result.FlightFavorite = fav;
result.Date = result.FlightFavorite.ArrivalOrDepartDateTime.ToString("dd/MM/yyyy");
result.VisibleFavorite = true;
return result;
}
}
}
catch
{
return result;
}
}