public async Task FlushFavoritesAsync() { await Initialization; var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple }; try { using (Stream outStream = await _favoritesFile.OpenStreamForWriteAsync()) using (var gzip = new GZipStream(outStream, CompressionLevel.Optimal)) { gzip.SerializeJsonToStream(_favorites, settings); } } catch (IOException ex) { //todo: Log me. Should we inform the user? There's nothing they can do, but info might be handy. System.Diagnostics.Debug.WriteLine($"Could not serialize favorites: {ex}: {ex.Message}"); } }
public override async Task OnNavigatedFromAsync(IDictionary <string, object> suspensionState, bool suspending) { if (suspending) { IStorageFile tripResultCacheFile = await _fileService.GetTempFileAsync( SuspensionKeys.TripResults_HasSavedState, CreationCollisionOption.ReplaceExisting); using (Stream fileStream = await tripResultCacheFile.OpenStreamForWriteAsync()) using (GZipStream jsonStream = new GZipStream(fileStream, CompressionLevel.Fastest)) { jsonStream.SerializeJsonToStream(TripResults.ToArray()); } //and make a note of it in the suspension dict: suspensionState.AddOrUpdate(SuspensionKeys.TripResults_HasSavedState, true); suspensionState.AddOrUpdate(SuspensionKeys.TripResults_FromName, FromName); suspensionState.AddOrUpdate(SuspensionKeys.TripResults_ToName, ToName); } else //Don't want to unhook the "back-returns from Detailed View" behavior if the user is just switching apps. { BootStrapper.BackRequested -= BootStrapper_BackRequested; } await Task.CompletedTask; }