예제 #1
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);
		}
        // Send a message to the socket.
        private async void SendMessageText()
        {
            string msg = SendMessageTextBox.Text;

            if (msg.Length > 0)
            {
                var msgLength = dataWriter.MeasureString(msg);
                dataWriter.WriteInt32(msg.Length);
                dataWriter.WriteString(msg);
                try
                {
                    await dataWriter.StoreAsync();

                    WriteMessageText("Message sent: " + msg + "\n");
                }
                catch (Exception e)
                {
                    WriteMessageText("Send error: " + e.Message + "\n");
                }
            }
        }
		public async void SaveData() {
			var fileList = await ApplicationData.Current.LocalFolder.GetFilesAsync();
			System.IO.MemoryStream ms = new System.IO.MemoryStream();
			var scratchFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(localScratchFileName, CreationCollisionOption.ReplaceExisting);
			using (var storageTransaction = await scratchFile.OpenTransactedWriteAsync()) {
				using (var dw = new Windows.Storage.Streams.DataWriter(storageTransaction.Stream)) {
					dw.WriteInt32(Items.Count);
					for (int x = 0; x < Items.Count; x++) {
						TrackItem item = Items[x];
						dw.WriteDateTime(item.Start);
						dw.WriteDateTime(item.End.Value);
						dw.WriteInt32(item.Topic.Length);
						dw.WriteString(item.Topic);
					}
					if (CurrentItem == null) {
						dw.WriteBoolean(false);
					} else {
						dw.WriteBoolean(true);
						dw.WriteDateTime(CurrentItem.Start);
						dw.WriteInt32(CurrentItem.Topic.Length);
						dw.WriteString(CurrentItem.Topic);
					}
					storageTransaction.Stream.Size = await dw.StoreAsync();
					await storageTransaction.CommitAsync();
				}
			}
			await scratchFile.RenameAsync(localFileName, NameCollisionOption.ReplaceExisting);
		}