Пример #1
0
        private async void LoadImage()
        {
            if (_currentTask != null)
            {
                _currentTask.Cancel();
            }

            TaskParameter imageLoader = null;

            var ffSource = await FFImageSourceBinding.GetImageSourceBinding(Source);

            if (ffSource == null)
            {
                if (internalImage != null)
                {
                    await MainThreadDispatcher.Instance.PostAsync(() => {
                        internalImage.Source = null;
                    });
                }
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Url)
            {
                imageLoader = ImageService.LoadUrl(ffSource.Path, TimeSpan.FromDays(CacheDuration));
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.CompiledResource)
            {
                imageLoader = ImageService.LoadCompiledResource(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.ApplicationBundle)
            {
                imageLoader = ImageService.LoadFileFromApplicationBundle(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Filepath)
            {
                imageLoader = ImageService.LoadFile(ffSource.Path);
            }

            if (imageLoader != null)
            {
                // CustomKeyFactory
                if (CacheKeyFactory != null)
                {
                    var dataContext = DataContext;
                    imageLoader.CacheKey(CacheKeyFactory.GetKey(Source, dataContext));
                }

                // LoadingPlaceholder
                if (LoadingPlaceholder != null)
                {
                    var placeholderSource = await FFImageSourceBinding.GetImageSourceBinding(LoadingPlaceholder);

                    if (placeholderSource != null)
                    {
                        imageLoader.LoadingPlaceholder(placeholderSource.Path, placeholderSource.ImageSource);
                    }
                }

                // ErrorPlaceholder
                if (ErrorPlaceholder != null)
                {
                    var placeholderSource = await FFImageSourceBinding.GetImageSourceBinding(ErrorPlaceholder);

                    if (placeholderSource != null)
                    {
                        imageLoader.ErrorPlaceholder(placeholderSource.Path, placeholderSource.ImageSource);
                    }
                }

                // Downsample
                if (DownsampleToViewSize && (Width > 0 || Height > 0))
                {
                    if (Height > Width)
                    {
                        imageLoader.DownSample(height: Height.PointsToPixels());
                    }
                    else
                    {
                        imageLoader.DownSample(width: Width.PointsToPixels());
                    }
                }
                else if (DownsampleToViewSize && (MinWidth > 0 || MinHeight > 0))
                {
                    if (MinHeight > MinWidth)
                    {
                        imageLoader.DownSample(height: MinHeight.PointsToPixels());
                    }
                    else
                    {
                        imageLoader.DownSample(width: MinWidth.PointsToPixels());
                    }
                }
                else if ((int)DownsampleHeight != 0 || (int)DownsampleWidth != 0)
                {
                    if (DownsampleHeight > DownsampleWidth)
                    {
                        imageLoader.DownSample(height: DownsampleUseDipUnits
                            ? DownsampleHeight.PointsToPixels() : (int)DownsampleHeight);
                    }
                    else
                    {
                        imageLoader.DownSample(width: DownsampleUseDipUnits
                            ? DownsampleWidth.PointsToPixels() : (int)DownsampleWidth);
                    }
                }

                // Downsample mode
                imageLoader.DownSampleMode(DownsampleMode);

                // RetryCount
                if (RetryCount > 0)
                {
                    imageLoader.Retry(RetryCount, RetryDelay);
                }

                // FadeAnimation
                imageLoader.FadeAnimation(FadeAnimationEnabled);

                // TransformPlaceholders
                imageLoader.TransformPlaceholders(TransformPlaceholders);

                // Transformations
                if (Transformations != null && Transformations.Count != 0)
                {
                    imageLoader.Transform(Transformations);
                }

                imageLoader.Finish((work) =>
                                   OnFinish(new Args.FinishEventArgs(work)));

                imageLoader.Success((imageSize, loadingResult) =>
                                    OnSuccess(new Args.SuccessEventArgs(imageSize, loadingResult)));

                imageLoader.Error((exception) =>
                                  OnError(new Args.ErrorEventArgs(exception)));

                _currentTask = imageLoader.Into(internalImage);
            }
        }
Пример #2
0
        private async void LoadImage()
        {
            if (_currentTask != null)
            {
                _currentTask.Cancel();
            }

            TaskParameter imageLoader = null;

            var ffSource = await FFImageSourceBinding.GetImageSourceBinding(Source);

            if (ffSource == null)
            {
                if (internalImage != null)
                {
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
                        internalImage.Source = null;
                    });
                }
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Url)
            {
                imageLoader = ImageService.LoadUrl(ffSource.Path, TimeSpan.FromDays(CacheDuration));
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.CompiledResource)
            {
                imageLoader = ImageService.LoadCompiledResource(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.ApplicationBundle)
            {
                imageLoader = ImageService.LoadFileFromApplicationBundle(ffSource.Path);
            }
            else if (ffSource.ImageSource == FFImageLoading.Work.ImageSource.Filepath)
            {
                imageLoader = ImageService.LoadFile(ffSource.Path);
            }

            if (imageLoader != null)
            {
                // LoadingPlaceholder
                if (LoadingPlaceholder != null)
                {
                    var placeholderSource = await FFImageSourceBinding.GetImageSourceBinding(LoadingPlaceholder);

                    if (placeholderSource != null)
                    {
                        imageLoader.LoadingPlaceholder(placeholderSource.Path, placeholderSource.ImageSource);
                    }
                }

                // ErrorPlaceholder
                if (ErrorPlaceholder != null)
                {
                    var placeholderSource = await FFImageSourceBinding.GetImageSourceBinding(ErrorPlaceholder);

                    if (placeholderSource != null)
                    {
                        imageLoader.ErrorPlaceholder(placeholderSource.Path, placeholderSource.ImageSource);
                    }
                }

                // Downsample
                if (DownsampleToViewSize && (Width > 0 || Height > 0))
                {
                    if (Height > Width)
                    {
                        imageLoader.DownSample(height: Height.PointsToPixels());
                    }
                    else
                    {
                        imageLoader.DownSample(width: Width.PointsToPixels());
                    }
                }
                else if (DownsampleToViewSize && (MinWidth > 0 || MinHeight > 0))
                {
                    if (MinHeight > MinWidth)
                    {
                        imageLoader.DownSample(height: MinHeight.PointsToPixels());
                    }
                    else
                    {
                        imageLoader.DownSample(width: MinWidth.PointsToPixels());
                    }
                }
                else if ((int)DownsampleHeight != 0 || (int)DownsampleWidth != 0)
                {
                    if (DownsampleHeight > DownsampleWidth)
                    {
                        imageLoader.DownSample(height: DownsampleUseDipUnits
                            ? DownsampleHeight.PointsToPixels() : (int)DownsampleHeight);
                    }
                    else
                    {
                        imageLoader.DownSample(width: DownsampleUseDipUnits
                            ? DownsampleWidth.PointsToPixels() : (int)DownsampleWidth);
                    }
                }

                // Downsample mode
                imageLoader.DownSampleMode(DownsampleMode);

                // RetryCount
                if (RetryCount > 0)
                {
                    imageLoader.Retry(RetryCount, RetryDelay);
                }

                // FadeAnimation
                imageLoader.FadeAnimation(FadeAnimationEnabled);

                // TransformPlaceholders
                imageLoader.TransformPlaceholders(TransformPlaceholders);

                // Transformations
                if (Transformations != null && Transformations.Count != 0)
                {
                    imageLoader.Transform(Transformations);
                }

                _currentTask = imageLoader.Into(internalImage);
            }
        }