Ejemplo n.º 1
0
        /// <summary>
        /// Called when <see cref="M:LoadDataAsync"/> is executed to load an object from persistent storage.
        /// </summary>
        /// <typeparam name="T">The type of the stored value.</typeparam>
        /// <param name="path">The string that identifies where the object is stored.</param>
        /// <param name="serializer">The implementation of <see cref="Wygwam.Windows.Storage.IDataSerializer"/> that will be used to deserialize the specified object.</param>
        /// <param name="storageType">Defines the desired storage type. Not all implementations support all storage types.</param>
        /// <returns>
        ///   <c>true</c> if the object was successfully retrieved.
        /// </returns>
        protected override async Task <T> OnLoadDataAsync <T>(string path, Wygwam.Windows.Storage.IDataSerializer serializer, Storage.StorageType storageType)
        {
            try
            {
                var file = await(await GetDataFolder(storageType, path)).GetFileAsync(Path.GetFileName(path));

                using (var inStream = await file.OpenSequentialReadAsync())
                {
                    return(serializer.Deserialize <T>(inStream.AsStreamForRead()));
                }
            }
            catch
            {
                return(default(T));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Called when <see cref="M:SaveDataAsync" /> is executed to store an object in persistent storage.
        /// </summary>
        /// <param name="path">The string that identifies where the object will be stored.</param>
        /// <param name="data">The object that will be serialized and stored.</param>
        /// <param name="serializer">The implementation of <see cref="Wygwam.Windows.Storage.IDataSerializer" /> that will be used to serialize the specified object.</param>
        /// <param name="storageType">Defines the desired storage type. Not all implementations support all storage types.</param>
        /// <returns>
        ///   <c>true</c> if the object was successfully persisted.
        /// </returns>
        protected override async Task <bool> OnSaveDataAsync(string path, object data, Wygwam.Windows.Storage.IDataSerializer serializer, Storage.StorageType storageType)
        {
            try
            {
                var file = await(await GetDataFolder(storageType, path)).CreateFileAsync(Path.GetFileName(path), _defaultCollisionPolicy);

                using (var fileStream = await file.OpenStreamForWriteAsync())
                {
                    serializer.Serialize(fileStream, data);
                    await fileStream.FlushAsync();
                }

                return(true);
            }
            catch
            {
            }

            return(false);
        }