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. } } }
private async Task UpdateSourceAsync() { _gifPresenter?.StopAnimation(); GifImage.Source = null; _gifPresenter = null; if (UriSource != null) { var uriSource = UriSource; try { var streamReference = RandomAccessStreamReference.CreateFromUri(uriSource); var readStream = await streamReference.OpenReadAsync(); if (readStream.ContentType.ToLowerInvariant() != "image/gif") { //GifImage.Source = new BitmapImage(uriSource); //throw new ArgumentException("Unsupported content type: " + readStream.ContentType); return; } using (readStream) using (var inMemoryStream = new InMemoryRandomAccessStream()) { await RandomAccessStream.CopyAndCloseAsync( readStream.GetInputStreamAt(0L), inMemoryStream.GetOutputStreamAt(0L) ); if (uriSource.Equals(UriSource)) { var gifPresenter = new GifPresenter(); GifImage.Source = await gifPresenter.InitializeAsync(inMemoryStream); if (uriSource.Equals(UriSource)) { _gifPresenter?.StopAnimation(); _gifPresenter = gifPresenter; if (_isLoaded) { _gifPresenter.StartAnimation(); } } } } } catch (FileNotFoundException) { // Just keep the empty image source. } } }