Exemple #1
0
        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 void SetSource(object source)
        {
            if (!IsInitialized)
            {
                return;
            }

            this._tokenSource?.Cancel();

            this._tokenSource = new CancellationTokenSource();

            AttachSource(null);

            if (source == null)
            {
                VisualStateManager.GoToState(this, UnloadedState, true);
                return;
            }

            VisualStateManager.GoToState(this, LoadingState, true);

            var imageSource = source as ImageSource;

            if (imageSource != null)
            {
                AttachSource(imageSource);

                ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs());
                VisualStateManager.GoToState(this, LoadedState, true);
                return;
            }

            _uri = source as Uri;
            if (_uri == null)
            {
                var url = source as string ?? source.ToString();
                if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out _uri))
                {
                    VisualStateManager.GoToState(this, FailedState, true);
                    return;
                }
            }

            _isHttpSource = IsHttpUri(_uri);
            if (!_isHttpSource && !_uri.IsAbsoluteUri)
            {
                _uri = new Uri("ms-appx:///" + _uri.OriginalString.TrimStart('/'));
            }

            await LoadImageAsync(_uri);
        }
        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 OnImageOpened(object sender, RoutedEventArgs e)
 {
     ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs());
     VisualStateManager.GoToState(this, LoadedState, true);
 }