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