LoadAsync() публичный Метод

public LoadAsync ( [ inputStream ) : IAsyncActionWithProgress
inputStream [
Результат IAsyncActionWithProgress
Пример #1
0
 public static async Task<InkStrokeContainer> LoadInkingFromFile(int pageNumber, StorageFolder dataFolder)
 {
     InkStrokeContainer inkStrokeContainer = new InkStrokeContainer();
     StorageFolder inkingFolder = await getInkingFolder(dataFolder);
     StorageFile inkFile = await inkingFolder.TryGetItemAsync(pageNumber.ToString() + EXT_INKING) as StorageFile;
     if (inkFile != null)
     {
         try
         {
             using (var inkStream = await inkFile.OpenSequentialReadAsync())
             {
                 await inkStrokeContainer.LoadAsync(inkStream);
             }
             AppEventSource.Log.Debug("InAppInking: Inking for page " + pageNumber.ToString() + " loaded.");
         }
         catch (Exception e)
         {
             string errorMsg = "Error when loading inking for page " + pageNumber.ToString() + "\n Exception: " + e.Message;
             AppEventSource.Log.Error("In App Inking: " + errorMsg);
             int userResponse = await App.NotifyUserWithOptions(errorMsg, new string[] { "Remove Inking", "Ignore" });
             switch (userResponse)
             {
                 case 0: // Delete inking file
                     await inkFile.DeleteAsync();
                     AppEventSource.Log.Error("InAppInking: File deleted.");
                     break;
                 default: break;
             }
         }
     }
     return inkStrokeContainer;
 }
        /// <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;
        }
Пример #3
0
        public async Task<Dictionary<int, InkStrokeContainer>> LoadInkDictionary()
        {
            InkDictionary = new Dictionary<int, InkStrokeContainer>();
            foreach (StorageFile inkFile in await InkingFolder.GetFilesAsync())
            {
                int pageNumber = 0;
                try
                {
                    pageNumber = Convert.ToInt32(inkFile.Name.Substring(0, inkFile.Name.Length - 4));
                    InkStrokeContainer inkStrokeContainer = new InkStrokeContainer();
                    using (var inkStream = await inkFile.OpenSequentialReadAsync())
                    {
                        await inkStrokeContainer.LoadAsync(inkStream);
                    }
                    InkDictionary.Add(pageNumber, inkStrokeContainer);

                }
                catch (Exception e)
                {
                    string errorMsg = "Error when loading inking for page " + pageNumber.ToString() + "\n Exception: " + e.Message;
                    AppEventSource.Log.Error("InkManager: " + errorMsg);
                    int userResponse = await App.NotifyUserWithOptions(errorMsg, new string[] { "Remove Inking", "Ignore" });
                    switch (userResponse)
                    {
                        case 0: // Delete inking file
                            await inkFile.DeleteAsync();
                            AppEventSource.Log.Error("InkManager: File deleted.");
                            break;
                        default: break;
                    }
                    continue;
                }
            }
            return InkDictionary;
        }