Esempio n. 1
0
		private static Session LoadSession(Windows.Storage.Streams.DataReader dr) {
			Session rv = new Session();
			rv.SessionId = dr.ReadGuid();
			rv.SessionDate = dr.ReadDateTime();
			int numberOfReadings = dr.ReadInt32();
			rv.Readings = new List<Reading>(numberOfReadings);
			for (int x = 0; x < numberOfReadings; x++) {
				rv.Readings.Add(Reading.LoadReading(dr));
			}
			return rv;
		}
Esempio n. 2
0
		/// <summary>
		/// Writes an array of Session objects to local storage.
		/// </summary>
		/// <param name="sessions">The Session objects to write. This is an array to force it to be a fixed collection by the time it gets here.</param>
		/// <returns>A task that can be awaited until the storage completes.</returns>
		public static async Task SerializeLocalSessions(Session[] sessions) {
			var fileList = await ApplicationData.Current.LocalFolder.GetFilesAsync();
			var scratchFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(localScratchSessionsFileName, CreationCollisionOption.ReplaceExisting);
			using (var storageTransaction = await scratchFile.OpenTransactedWriteAsync()) {
				using (var dw = new Windows.Storage.Streams.DataWriter(storageTransaction.Stream)) {
					dw.WriteInt32(sessions.Length);
					for (int x = 0; x < sessions.Length; x++) {
						sessions[x].WriteSession(dw);
					}
					storageTransaction.Stream.Size = await dw.StoreAsync();
					await storageTransaction.CommitAsync();
				}
			}
			await scratchFile.RenameAsync(localSessionsFileName, NameCollisionOption.ReplaceExisting);
		}