Beispiel #1
0
        private async void tryStart()
        {
            if (processing < config.ThreadPoolSize)
            {
                if (queue.Count > 0)
                {
                    processing++;
                    ImageLoaderQueueItem item = null;
                    if (config.QueueProcessingType == QueueProcessingType.FIFO)
                    {
                        item = queue[0];
                    }
                    else
                    {
                        item = queue[queue.Count - 1];
                    }

                    await doProcess(item);

                    queue.Remove(item);//处理完了后,再删除

                    processing--;
                    tryStart();//[触发2]每完成一个任务后尝试启动新任何
                }
            }
        }
Beispiel #2
0
        private void doShow(ImageLoaderQueueItem item, BitmapImage image)
        {
            if (item.View != null && image != null)
            {
                Util.call(() =>
                {
                    item.View.Source = image;
                });
            }

            if (item.Listener != null)
            {
                Util.call(() =>
                {
                    if (image == null)
                    {
                        item.Listener(LoadingState.Failed, item.Url, item.View, image);
                    }
                    else
                    {
                        item.Listener(LoadingState.Completed, item.Url, item.View, image);
                    }
                });
            }
        }
        public ImageLoaderQueueItem DownloadImage(string url, DisplayImageOptions options, ImageLoadingListener listener) {
            if (string.IsNullOrEmpty(url))
                return null;

            var item = new ImageLoaderQueueItem() { Url = url, Options = options, View = null, Listener = listener };
            return add(item);
        }
Beispiel #4
0
        private async Task doSave(ImageLoaderQueueItem item, IBuffer buffer)
        {
            if (buffer == null)
            {
                return;
            }

            if (item.Options.CacheOnDisk)
            {
                await config.DiskCache.Save(item.Url, buffer);
            }
        }
Beispiel #5
0
        public ImageLoaderQueueItem DownloadImage(string url, DisplayImageOptions options, ImageLoadingListener listener)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(null);
            }

            var item = new ImageLoaderQueueItem()
            {
                Url = url, Options = options, View = null, Listener = listener
            };

            return(add(item));
        }
Beispiel #6
0
        private async Task doProcess(ImageLoaderQueueItem item)
        {
            IBuffer     buffer = null;
            BitmapImage image  = null;

            if (buffer == null)
            {
                item.Listener(LoadingState.Started, item.Url, item.View, null);

                buffer = await config.ImageDownloader.download(item.Url, item.Options.ExtraForDownloader);

                image = await doDecode(item, buffer);
                await doSave(item, buffer);
            }

            doShow(item, image);
        }
Beispiel #7
0
        //---------
        private ImageLoaderQueueItem add(ImageLoaderQueueItem item)
        {
            item.Code = item.Url.GetHashCode();

            if (queue.FirstOrDefault(m => m.Code == item.Code) != null)
            {
                if (item.Listener != null)
                {
                    item.Listener(LoadingState.Cancelled, item.Url, item.View, null);
                }
                return(item);
            }


            doTryAdd(item);

            return(item);
        }
Beispiel #8
0
        private async Task <BitmapImage> doDecode(ImageLoaderQueueItem item, IBuffer buffer)
        {
            if (buffer != null)
            {
                try {
                    InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
                    DataWriter datawriter             = new DataWriter(stream.GetOutputStreamAt(0));
                    datawriter.WriteBuffer(buffer, 0, buffer.Length);
                    await datawriter.StoreAsync();

                    BitmapImage image = new BitmapImage();
                    image.SetSource(stream);

                    return(image);
                }
                catch { }
            }

            return(null);
        }
Beispiel #9
0
        private async void doTryAdd(ImageLoaderQueueItem item)
        {
            if (item.Options.CacheOnDisk)
            {
                if (config.DiskCache != null)
                {
                    var buffer = await config.DiskCache.Get(item.Url);

                    var image = await doDecode(item, buffer);

                    if (buffer != null)
                    {
                        doShow(item, image);
                        return;
                    }
                }
            }

            queue.Add(item); //
            tryStart();      //[触发1]每次添加都尝试启动任务
        }
        private void DoUpdateSourceAsync()
        {
            if (Src == null || Src.Scheme == null)
            {
                return;
            }

            if (Src.Scheme.IndexOf("http") < 0)  //如果不是http地址
            {
                view.Source = new BitmapImage(Src);
            }
            else  //如果是http地址
            {
                view.Source = null;
                var loader = CurrentLoader();
                loadingItem = loader.DownloadImage(Src.AbsoluteUri, (state, url, v, image) =>
                {
                    if (_isLoaded && state == LoadingState.Completed)
                    {
                        view.Source = image;
                    }
                });
            }
        }
        private async Task<BitmapImage> doDecode(ImageLoaderQueueItem item, IBuffer buffer) {
            if (buffer != null) {
                try {
                    InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
                    DataWriter datawriter = new DataWriter(stream.GetOutputStreamAt(0));
                    datawriter.WriteBuffer(buffer, 0, buffer.Length);
                    await datawriter.StoreAsync();

                    BitmapImage image = new BitmapImage();
                    image.SetSource(stream);

                    return image;
                }
                catch { }
            }

            return null;
        }
        private async Task doSave(ImageLoaderQueueItem item, IBuffer buffer) {
            if (buffer == null)
                return;

            if (item.Options.CacheOnDisk) {
                await config.DiskCache.Save(item.Url, buffer);
            }
        }
        private void doShow(ImageLoaderQueueItem item, BitmapImage image) {
            if (item.View != null && image != null) {
                Util.call(() =>
                {
                    item.View.Source = image;
                });
            }

            if (item.Listener != null) {
                Util.call(() =>
                {
                    if (image == null)
                        item.Listener(LoadingState.Failed, item.Url, item.View, image);
                    else
                        item.Listener(LoadingState.Completed, item.Url, item.View, image);
                });
            }
        }
        private async Task doProcess(ImageLoaderQueueItem item) {
            IBuffer buffer = null;
            BitmapImage image = null;
            
            if (buffer == null) {
                item.Listener(LoadingState.Started, item.Url, item.View, null);

                buffer = await config.ImageDownloader.download(item.Url, item.Options.ExtraForDownloader);
                image = await doDecode(item, buffer);
                await doSave(item, buffer);
            }

            doShow(item, image);
        }
        private async void doTryAdd(ImageLoaderQueueItem item) {
            if (item.Options.CacheOnDisk) {
                if (config.DiskCache != null) {
                    var buffer = await config.DiskCache.Get(item.Url);
                    var image = await doDecode(item, buffer);

                    if (buffer != null) {
                        doShow(item, image);
                        return;
                    }
                }
            }

            queue.Add(item); //
            tryStart();//[触发1]每次添加都尝试启动任务
        }
Beispiel #16
0
 public void Remove(ImageLoaderQueueItem item)
 {
     queue.Remove(item);
 }
 public void Remove(ImageLoaderQueueItem item) {
     queue.Remove(item);
 }
        //---------
        private ImageLoaderQueueItem add(ImageLoaderQueueItem item) {
            item.Code = item.Url.GetHashCode();

            if (queue.FirstOrDefault(m => m.Code == item.Code) != null) {
                if (item.Listener != null) {
                    item.Listener(LoadingState.Cancelled, item.Url, item.View, null);
                }
                return item;
            }


            doTryAdd(item);

            return item;
        }
        private void DoUpdateSourceAsync() {
            if (Src == null || Src.Scheme == null)
                return;

            if (Src.Scheme.IndexOf("http") < 0) {//如果不是http地址
                view.Source = new BitmapImage(Src);
            }
            else {//如果是http地址
                view.Source = null;
                var loader = CurrentLoader();
                loadingItem = loader.DownloadImage(Src.AbsoluteUri, (state, url, v, image) =>
                {
                    if (_isLoaded && state == LoadingState.Completed) {
                        view.Source = image;
                    }
                });
            }
        }