示例#1
0
        private void OnUriSourceChanged()
        {
            if (Parent == null)
            {
                // If the parent is not present we can wait for OnAttachedToWindow to call again.
                return;
            }

            ResetImage();

            if (!UriSource.HasValue())
            {
                return;
            }
            var newUri = new Uri(UriSource);

            if (newUri.Scheme == "resource" ||
                newUri.IsFile ||
                newUri.IsLocalResource())
            {
                SetImageResource(GetResourceId(newUri.PathAndQuery.TrimStart(new[] { '/' })));
            }
            else if (UriSource.StartsWith("res:///", StringComparison.OrdinalIgnoreCase))
            {
                int resourceId;

                if (int.TryParse(UriSource.Replace("res:///", ""), out resourceId))
                {
                    SetImageResource(resourceId);
                }
                else
                {
                    this.Log().WarnFormat("Failed to load asset resource [{0}]", UriSource);
                }
            }
            else if (UriSource.StartsWith("file://", StringComparison.OrdinalIgnoreCase) || newUri.IsAppData())
            {
                if (_targetHeight <= 0 || _targetWidth <= 0)
                {
                    _sourceUpdateRequired = true;
                    return;
                }

                var filePath = newUri.IsAppData() ?
                               AppDataUriEvaluator.ToPath(newUri) :
                               UriSource.TrimStart("file://", StringComparison.OrdinalIgnoreCase);

                var options = new BitmapFactory.Options();
                options.InJustDecodeBounds = true;
                BitmapFactory.DecodeFile(filePath, options).SafeDispose();
                var sampleSize = CalculateInSampleSize(options, _targetWidth, _targetHeight);

                options.InJustDecodeBounds = false;
#pragma warning disable CS0618 // Type or member is obsolete
                options.InPurgeable = true;
#pragma warning restore CS0618 // Type or member is obsolete
                options.InSampleSize = sampleSize;

                var bitmap = BitmapFactory.DecodeFile(filePath, options);

                if (_currentBitmap != bitmap)
                {
                    SetImageBitmap(bitmap);
                }
            }
            else
            {
                _download.Disposable = CoreDispatcher.Main
                                       .RunAsync(
                    CoreDispatcherPriority.Normal,
                    async(ct) =>
                {
                    var localUri = UriSource;

                    using (var b = await DownloadImage(localUri, _targetWidth, _targetHeight, ct))
                    {
                        if (!ct.IsCancellationRequested && b != null)
                        {
                            if (_currentBitmap != b)
                            {
                                SetImageBitmap(b);
                            }

                            if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                            {
                                this.Log().DebugFormat("Bitmap set {0}", localUri);
                            }

                            if (_isInteractive)
                            {
                                _saveScale = 1f;
                                OnDrawableChanged();
                            }
                        }
                        else if (b == null && ImageLoader == null)
                        {
                            // The bitmap may be null if the image loader has already set the image from

                            this.Log().Warn("Bitmap is null after image download");
                        }
                    }
                }
                    );
            }
        }
        private void OnUriSourceChanged()
        {
            ResetImage();

            if (!UriSource.HasValue())
            {
                return;
            }
            var newUri = new Uri(UriSource);

            if (newUri.Scheme == "resource")
            {
                Resource = GetResourceId(newUri.PathAndQuery.TrimStart('/'));
            }
            else if (UriSource.StartsWith("res:///", StringComparison.OrdinalIgnoreCase))
            {
                int resourceId;

                if (int.TryParse(UriSource.Replace("res:///", ""), out resourceId))
                {
                    Resource = resourceId;
                }
                else
                {
                    if (this.Log().IsEnabled(LogLevel.Warning))
                    {
                        this.Log().WarnFormat("Failed to load asset resource [{0}]", UriSource);
                    }
                }
            }
            else if (UriSource.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
            {
                var filePath = UriSource.TrimStart("file://", StringComparison.OrdinalIgnoreCase);

                var options = new BitmapFactory.Options();
                options.InJustDecodeBounds = true;
                BitmapFactory.DecodeFile(filePath, options).SafeDispose();
                var sampleSize = CalculateInSampleSize(options, TargetWidth, TargetHeight);

                options.InJustDecodeBounds = false;
                options.InPurgeable        = true;
                options.InSampleSize       = sampleSize;

                CurrentBitmap = BitmapFactory.DecodeFile(filePath, options);
            }
            else
            {
#if !IS_UNO
                _download.Disposable = new CoreDispatcherScheduler(CoreDispatcher.Main, CoreDispatcherPriority.Normal)
                                       .ScheduleAsync((object)null, async(s, state, ct) =>
                {
                    var localUri = UriSource;

                    var b = await DownloadImage(localUri, TargetWidth, TargetHeight, ct);
                    if (!ct.IsCancellationRequested && b != null)
                    {
                        CurrentBitmap = b;
                        ImageDownloaded();

                        if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                        {
                            this.Log().DebugFormat("Bitmap set {0}", localUri);
                        }
                    }
                    else if (b == null && ImageLoader == null)
                    {
                        // The bitmap may be null if the image loader has already set the image from

                        if (this.Log().IsEnabled(LogLevel.Warning))
                        {
                            this.Log().Warn("Bitmap is null after image download");
                        }
                    }
                }
                                                      );
#endif
            }
        }