0

我正在使用像缓存一样的本地存储来持久化数据:

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

    }

我需要本地存储每 24 小时过期一次以引用本地存储的值,请问你该怎么做?

问候

4

1 回答 1

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

}
于 2012-11-13T11:57:39.333 回答