public async Task saveInking(int pageNumber, InkStrokeContainer inkStrokeContainer) { // Remove if there is no strokes. if (inkStrokeContainer.GetStrokes().Count == 0) { InkDictionary.Remove(pageNumber); } else { InkDictionary[pageNumber] = inkStrokeContainer; } // Enqueue the page if it is not already in the queue if (!this.inkingChangedPagesQueue.Contains(pageNumber)) this.inkingChangedPagesQueue.Enqueue(pageNumber); // Invoke save inking only if SaveInking is not running. // This will prevent running multiple saving instance at the same time. if (!this.isSavingInking) await SaveInkingQueue(); }
/// <summary> /// Load the family and their notes from local storage /// </summary> /// <returns>Null if there was no model to load, otherwise, the deserialized model</returns> 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; }
public void SyncStrokeEx(Dictionary<InkStroke,double> strokes, InkStrokeContainer containner, double currentCanvasWidth,bool forcrRefresh = false) { lock(locker) { if(strokes.Count > containner.GetStrokes().Count) { foreach (var item in strokes) { bool exist = false; foreach (var itemInner in containner.GetStrokes()) { InkStroke strokeCopy = itemInner.Clone(); strokeCopy.PointTransform = System.Numerics.Matrix3x2.CreateScale(1f); if (InkHelper.SameInkStroke(item.Key, strokeCopy)) { exist = true; break; } } if (!exist) { foreach(var itemMp in StrokeMapping) { if(InkHelper.SameInkStroke(item.Key,itemMp.Key) && item.Value == itemMp.Value) { StrokeMapping.Remove(itemMp.Key); break; } } } } } else if(strokes.Count < containner.GetStrokes().Count) { InkStroke stroke = containner.GetStrokes()[containner.GetStrokes().Count - 1].Clone(); StrokeMapping.Add(stroke.Clone(),currentCanvasWidth); } else { if(strokes.Count == StrokeMapping.Count && !forcrRefresh) { return; } } strokes.Clear(); containner.Clear(); foreach(var item in StrokeMapping) { InkStroke stroke = item.Key.Clone(); strokes.Add(stroke.Clone(), item.Value); stroke.PointTransform = System.Numerics.Matrix3x2.CreateScale((float)(currentCanvasWidth / item.Value)); containner.AddStroke(stroke.Clone()); } //if(read) //{ // strokes.Clear(); // foreach(var item in StrokeMapping) // { // strokes.Add(item.Key.Clone(),item.Value); // } //} //else //{ // StrokeMapping.Clear(); // foreach(var item in strokes) // { // StrokeMapping.Add(item.Key.Clone(),item.Value); // } //} } }
public void SyncStroke(InkStrokeContainer containner,double currentCanvasWidth,bool syncAll = true, InkStroke stroke = null) { lock(locker) { try { if (syncAll) { Strokes.Clear(); foreach (var item in containner.GetStrokes()) { InkStroke strokeClone = item.Clone(); strokeClone.PointTransform = System.Numerics.Matrix3x2.CreateScale((float)(currentCanvasWidth / originalCanvasWidth)); Strokes.Add(strokeClone); } } else { if (null != stroke) { InkStroke strokeClone = stroke.Clone(); strokeClone.PointTransform = System.Numerics.Matrix3x2.CreateScale((float)(currentCanvasWidth / originalCanvasWidth)); Strokes.Add(strokeClone); } else { containner.Clear(); foreach (var item in Strokes) { InkStroke strokeClone = item.Clone(); strokeClone.PointTransform = System.Numerics.Matrix3x2.CreateScale((float)(currentCanvasWidth / originalCanvasWidth)); containner.AddStroke(strokeClone); } } } } catch(Exception e) { #if DEBUG System.Diagnostics.Debug.WriteLine(e.Message); #endif } } }