2012-11-13 23 views
0

我使用像緩存localStorage的留存數據:到期的localStorage + Windows Phone的

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

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

}