Exemplo n.º 1
0
        private async Task UpdateSourceAsync()
        {
            _imageDecoder?.Dispose();
            _initializationCancellationTokenSource?.Cancel();
            _image.Source    = null;
            this.PixelHeight = 0d;
            this.PixelWidth  = 0d;
            Interlocked.Exchange(ref _imageDecoder, null);
            if (UriSource != null)
            {
                var uriSource = UriSource;
                var cancellationTokenSource = new CancellationTokenSource();
                _initializationCancellationTokenSource = cancellationTokenSource;
                try
                {
                    this.OnLoadingStarted();
                    var imageSource = await LoadImageByUri(uriSource, cancellationTokenSource);

                    if (uriSource.Equals(UriSource))
                    {
                        _image.Source = imageSource;
                    }
                    else
                    {
                        //不处理此情况
                    }
                    this.OnLoadingCompleted(imageSource);
                }
                catch (TaskCanceledException)
                {
                    // Task Canceled
                }
                catch (FileNotFoundException fnfex)
                {
                    this.OnFail(fnfex);
                }
                catch (Exception ex)
                {
                    this.OnFail(ex);
                }
            }
        }