in FamilyNotes/App.xaml.cs [367:436]
private async Task<Model> LoadModelAsync()
{
Model model = null;
InkStrokeContainer combinedStrokes = new InkStrokeContainer(); // To avoid managing individual files for every InkCanvas, we will combine all ink stroke information into one container
List<int> InkStrokesPerCanvas = new List<int>();
try
{
StorageFile modelDataFile = await ApplicationData.Current.LocalFolder.GetFileAsync(NOTES_MODEL_FILE);
using (IRandomAccessStream randomAccessStream = await modelDataFile.OpenAsync(FileAccessMode.ReadWrite))
{
// Load the model which contains the people and the note collection
try
{
DataContractJsonSerializer modelSerializer = new DataContractJsonSerializer(typeof(Model));
model = (Model)modelSerializer.ReadObject(randomAccessStream.AsStream());
}
catch (System.Runtime.Serialization.SerializationException)
{
System.Diagnostics.Debug.Assert(false, "Failed to load serialized model");
return null;
}
}
// For each sticky note, load the number of inkstrokes it contains
StorageFile inkDataFile = await ApplicationData.Current.LocalFolder.GetFileAsync(NOTES_INK_FILE);
using (IInputStream inkStream = await inkDataFile.OpenSequentialReadAsync())
{
bool combinedStrokesExist = false;
DataReader reader = new DataReader(inkStream);
foreach (StickyNote n in model.StickyNotes)
{
await reader.LoadAsync(sizeof(int)); // You need to buffer the data before you can read from a DataReader.
int numberOfInkStrokes = reader.ReadInt32();
InkStrokesPerCanvas.Add(numberOfInkStrokes);
combinedStrokesExist |= numberOfInkStrokes > 0;
}
// Load the ink data
if (combinedStrokesExist)
{
await combinedStrokes.LoadAsync(inkStream);
}
} // using inkStream
} // try
catch (FileNotFoundException)
{
// No data to load. We'll start with a fresh model
return null;
}
// Factor out the inkstrokes from the big container into each note
int allStrokesIndex = 0, noteIndex = 0;
IReadOnlyList<InkStroke> allStrokes = combinedStrokes.GetStrokes();
foreach (StickyNote n in model.StickyNotes)
{
// InkStrokeContainers can't be serialized using the default xml/json serialization.
// So create a new one and fill it up from the data we restored
n.Ink = new InkStrokeContainer();
// pull out the ink strokes that belong to this note
for (int i = 0; i < InkStrokesPerCanvas[noteIndex]; i++)
{
n.Ink.AddStroke(allStrokes[allStrokesIndex++].Clone());
}
++noteIndex;
}
return model;
}