到期localstorage + windows phone

时间:2012-11-13 11:28:25

标签: c# silverlight xaml windows-phone-7.1 windows-phone-8

我正在使用像缓存这样的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小时到期一次,以获取本地存储的价值,你怎么能这样做?

问候

1 个答案:

答案 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;
        }

}