in FamilyNotes/App.xaml.cs [307:361]
public async Task SaveModelAsync()
{
// Persist the model
StorageFile notesDataFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(NOTES_MODEL_FILE, CreationCollisionOption.ReplaceExisting);
using (Stream notesDataStream = await notesDataFile.OpenStreamForWriteAsync())
{
// Serialize the model which contains the people and the stickyNote collection
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model));
serializer.WriteObject(notesDataStream, Model);
}
/* For each sticky note, save the number of inkstrokes it contains.
The function on the InkStrokeContainer that persists its contents is not designed
to save persist containers to the one stream. We also don't want to manage one
backing file per note. So combine the ink strokes into one container and persist that.
We'll seperate out the ink strokes to the right ink control by keeping track of how
many ink strokes belongs to each note */
InkStrokeContainer CombinedStrokes = new InkStrokeContainer();
StorageFile inkFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(NOTES_INK_FILE, CreationCollisionOption.ReplaceExisting);
using (var randomAccessStream = await inkFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (IOutputStream inkStream = randomAccessStream.GetOutputStreamAt(0)) // DataWriter requires an IOutputStream
{
bool combinedStrokesHasContent = false; // whether we had any ink to save across all of the notes
DataWriter writer = new DataWriter(inkStream);
foreach (StickyNote Note in Model.StickyNotes)
{
// Save # strokes for this note
if (Note.Ink != null && Note.Ink.GetStrokes().Count > 0)
{
IReadOnlyList<InkStroke> InkStrokesInNote = Note.Ink.GetStrokes();
writer.WriteInt32(InkStrokesInNote.Count);
// capture the ink strokes into the combined container which will be saved at the end of the notes data file
foreach (InkStroke s in InkStrokesInNote)
{
CombinedStrokes.AddStroke(s.Clone());
}
combinedStrokesHasContent = true;
}
else
{
writer.WriteInt32(0); // not all notes have ink
}
}
await writer.StoreAsync(); // flush the data in the writer to the inkStream
// Persist the ink data
if (combinedStrokesHasContent )
{
await CombinedStrokes.SaveAsync(inkStream);
}
}
}
}