private async Task LoadImageAsync() { if (_uri != null) { if (IsCacheEnabled && _isHttpSource) { try { var img = await ImageCache.Instance.GetFromCacheAsync(_uri, true, _tokenSource.Token); _image.Source = img; ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs()); VisualStateManager.GoToState(this, LoadedState, true); } catch (OperationCanceledException) { // nothing to do as cancellation has been requested. } catch (Exception e) { ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(e)); VisualStateManager.GoToState(this, FailedState, true); } } else { _image.Source = new BitmapImage(_uri); } } }
private async Task LoadImageAsync() { if (!_isLoadingImage && _uri != null) { _isLoadingImage = true; if (IsCacheEnabled && _isHttpSource) { try { _image.Source = await ImageCache.GetFromCacheAsync(_uri, true); ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs()); VisualStateManager.GoToState(this, LoadedState, true); } catch (Exception e) { ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(e)); VisualStateManager.GoToState(this, FailedState, true); } } else { _image.Source = new BitmapImage(_uri); } _isLoadingImage = false; } }
private async Task SetHttpSourceCustomCached(Uri imageUri) { try { var propValues = new List <KeyValuePair <string, object> >(); if (DecodePixelHeight > 0) { propValues.Add(new KeyValuePair <string, object>(nameof(DecodePixelHeight), DecodePixelHeight)); } if (DecodePixelWidth > 0) { propValues.Add(new KeyValuePair <string, object>(nameof(DecodePixelWidth), DecodePixelWidth)); } if (propValues.Count > 0) { propValues.Add(new KeyValuePair <string, object>(nameof(DecodePixelType), DecodePixelType)); } var img = await ImageCache.Instance.GetFromCacheAsync(imageUri, true, _tokenSource.Token, propValues); lock (LockObj) { // If you have many imageEx in a virtualized listview for instance // controls will be recycled and the uri will change while waiting for the previous one to load if (_uri == imageUri) { AttachSource(img); ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs()); VisualStateManager.GoToState(this, LoadedState, true); } } } catch (OperationCanceledException) { // nothing to do as cancellation has been requested. } catch (Exception e) { lock (LockObj) { if (_uri == imageUri) { ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(e)); VisualStateManager.GoToState(this, FailedState, true); } } } }
private async Task LoadImageAsync() { if (_uri != null) { if (IsCacheEnabled && _isHttpSource) { var ogUri = _uri; try { var img = await ImageCache.Instance.GetFromCacheAsync(ogUri, Path.GetFileName(ogUri.ToString()), true); lock (_lockObj) { // If you have many imageEx in a virtualized listview for instance // controls will be recycled and the uri will change while waiting for the previous one to load if (_uri == ogUri) { _image.Source = img; ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs()); VisualStateManager.GoToState(this, LoadedState, true); } } } catch (Exception e) { lock (_lockObj) { if (_uri == ogUri) { ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(e)); VisualStateManager.GoToState(this, FailedState, true); } } } } else { _image.Source = new BitmapImage(_uri); } } }
private void OnImageFailed(object sender, ExceptionRoutedEventArgs e) { ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(new Exception(e.ErrorMessage))); VisualStateManager.GoToState(this, FailedState, true); }
private async void SetSource(object source) { if (!IsInitialized) { return; } _tokenSource?.Cancel(); _tokenSource = new CancellationTokenSource(); AttachSource(null); if (source == null) { return; } VisualStateManager.GoToState(this, LoadingState, true); var imageSource = source as ImageSource; if (imageSource != null) { AttachSource(imageSource); return; } var uri = source as Uri; if (uri == null) { var url = source as string ?? source.ToString(); if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uri)) { ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(new UriFormatException("Invalid uri specified."))); VisualStateManager.GoToState(this, FailedState, true); return; } } if (!IsHttpUri(uri) && !uri.IsAbsoluteUri) { uri = new Uri("ms-appx:///" + uri.OriginalString.TrimStart('/')); } try { await LoadImageAsync(uri, _tokenSource.Token); } catch (OperationCanceledException) { // nothing to do as cancellation has been requested. } catch (Exception e) { ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(e)); VisualStateManager.GoToState(this, FailedState, true); } }