0

データを永続化するために、キャッシュのような 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 時間ごとに localstorage の有効期限が切れる必要があります。

よろしく

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 に答える