private async Task UpdateSourceAsync()
        {
            _gifPresenter?.StopAnimation();
            _initializationCancellationTokenSource?.Cancel();

            GifImage.Source = null;
            _gifPresenter = null;

            if (UriSource != null)
            {
                var uriSource = UriSource;
                var cancellationTokenSource = new CancellationTokenSource();

                _initializationCancellationTokenSource = cancellationTokenSource;

                try
                {
                    var streamReference = RandomAccessStreamReference.CreateFromUri(uriSource);
                    var inMemoryStream = new InMemoryRandomAccessStream();

                    using (inMemoryStream)
                    {
                        var readStream = await streamReference.OpenReadAsync().AsTask(cancellationTokenSource.Token);

                        if (readStream.ContentType.ToLowerInvariant() != "image/gif")
                        {
                            throw new ArgumentException("Unsupported content type: " + readStream.ContentType);
                        }

                        var copyAction = RandomAccessStream.CopyAndCloseAsync(
                                readStream.GetInputStreamAt(0L),
                                inMemoryStream.GetOutputStreamAt(0L)
                                );
                        await copyAction.AsTask(cancellationTokenSource.Token);
                        
                        if (uriSource.Equals(UriSource))
                        {
                            var gifPresenter = new GifPresenter();

                            GifImage.Source = await gifPresenter.InitializeAsync(inMemoryStream);
                            _gifPresenter = gifPresenter;

                            if (_isLoaded)
                            {
                                _gifPresenter.StartAnimation();
                            }
                        }
                    }
                }
                catch (TaskCanceledException)
                {
                    // Just keep the empty image source.
                }
                catch (FileNotFoundException)
                {
                    // Just keep the empty image source.
                }
            }
        }
Esempio n. 2
0
        private async Task UpdateSourceAsync()
        {
            _gifPresenter?.StopAnimation();
            _initializationCancellationTokenSource?.Cancel();

            GifImage.Source = null;
            _gifPresenter   = null;

            if (UriSource != null)
            {
                var uriSource = UriSource;
                var cancellationTokenSource = new CancellationTokenSource();

                _initializationCancellationTokenSource = cancellationTokenSource;

                try
                {
                    var streamReference = RandomAccessStreamReference.CreateFromUri(uriSource);
                    var inMemoryStream  = new InMemoryRandomAccessStream();

                    using (inMemoryStream)
                    {
                        var readStream = await streamReference.OpenReadAsync().AsTask(cancellationTokenSource.Token);

                        if (readStream.ContentType.ToLowerInvariant() != "image/gif")
                        {
                            GifImage.Source = new BitmapImage(uriSource);
                            inMemoryStream.FlushAsync();
                            inMemoryStream.Dispose();
                            return;
                            //throw new ArgumentException("Unsupported content type: " + readStream.ContentType);
                        }

                        var copyAction = RandomAccessStream.CopyAndCloseAsync(
                            readStream.GetInputStreamAt(0L),
                            inMemoryStream.GetOutputStreamAt(0L)
                            );
                        await copyAction.AsTask(cancellationTokenSource.Token);

                        if (uriSource.Equals(UriSource))
                        {
                            var gifPresenter = new GifPresenter();

                            GifImage.Source = await gifPresenter.InitializeAsync(inMemoryStream);

                            _gifPresenter = gifPresenter;

                            if (_isLoaded)
                            {
                                _gifPresenter.StartAnimation();
                            }
                        }
                    }
                }
                catch (ArgumentException ex)
                {
                    //GifImage.Source = new BitmapImage(uriSource);
                }
                catch (TaskCanceledException)
                {
                    // Just keep the empty image source.
                }
                catch (FileNotFoundException)
                {
                    // Just keep the empty image source.
                }
            }
        }