Ejemplo n.º 1
0
        /// <summary>
        /// Loads image data from a local file and set it to current image data.
        /// </summary>
        /// <param name="item">Target item</param>
        internal async Task LoadSetAsync(FileItemViewModel item)
        {
            _tokenSourceLoading?.TryCancel();             // Canceling already canceled source will be simply ignored.

            try
            {
                _tokenSourceLoading = new CancellationTokenSourcePlus();

                byte[] data = null;
                if (item.IsAvailableLocal && item.CanLoadDataLocal)
                {
                    data = await FileAddition.ReadAllBytesAsync(ComposeLocalPath(item), _tokenSourceLoading.Token);
                }

                CurrentItem      = item;
                CurrentImageData = data;
            }
            catch (OperationCanceledException)
            {
                // None.
            }
            catch (FileNotFoundException)
            {
                item.Status       = FileStatus.NotCopied;
                item.IsAliveLocal = false;
            }
            catch (IOException)
            {
                item.IsAvailableLocal = false;
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Failed to load image data from local file.\r\n{ex}");
                throw new UnexpectedException("Failed to load image data from local file.", ex);
            }
            finally
            {
                _tokenSourceLoading?.Dispose();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Load image data from local file of a specified item and set it to current image data.
        /// </summary>
        /// <param name="item">Target item</param>
        private async Task LoadSetFileBaseAsync(FileItemViewModel item)
        {
            var localPath = ComposeLocalPath(item);

            try
            {
                tokenSourceLoading           = new CancellationTokenSource();
                isTokenSourceLoadingDisposed = false;

                byte[] data = null;
                if (item.CanLoadDataLocal)
                {
                    data = await FileAddition.ReadAllBytesAsync(localPath, tokenSourceLoading.Token);
                }

                CurrentItem      = item;
                CurrentImageData = data;
            }
            catch (FileNotFoundException)
            {
                item.Status       = FileStatus.NotCopied;
                item.IsAliveLocal = false;
            }
            catch (IOException)
            {
                item.CanLoadDataLocal = false;
            }
            finally
            {
                if (tokenSourceLoading != null)
                {
                    isTokenSourceLoadingDisposed = true;
                    tokenSourceLoading.Dispose();
                }
            }
        }