Пример #1
0
        public async Task LoadImageAndDetailTask()
        {
            var loadingsb = this.Sb("LoadingSb");

            loadingsb.Begin();
            this.Sb("LoadingStartSb").Begin();

            // 同时进行图片加载和详情页分析
            var  imgTask    = LoadImageAsync();
            Task detailTask = null;

            if (ImageItem.GetDetailTaskFunc != null)
            {
                detailTask = ImageItem.TryGetDetail();
            }

            // 等待图片加载和详情页分析完成
            var e = await imgTask;

            if (detailTask != null)
            {
                await detailTask;
            }

            var loadedsb = this.Sb("LoadedAllSb");

            loadedsb.Completed += (sender, args) =>
            {
                loadingsb.Stop();
            };
            loadedsb.Begin();

            // Load end
            if (e == null)
            {
                RefreshButton.Visibility = Visibility.Collapsed;
            }
            ImageLoadEnd?.Invoke(this);
        }
Пример #2
0
        public async Task LoadImageAsync()
        {
            var loadingsb = this.Sb("LoadingSb");

            loadingsb.Begin();
            this.Sb("LoadingStartSb").Begin();

            // client
            var net = ImageItem.Net ?? new NetSwap(Settings);

            net.SetTimeOut(15);
            net.SetReferer(ImageItem.ThumbnailUrlInfo.Referer);
            Exception loadex = null;

            try
            {
                var cts           = new CancellationTokenSource(TimeSpan.FromSeconds(15));
                var getDetailTask = ImageItem.GetDetailAsync();
                var response      = await net.Client.GetAsync(ImageItem.ThumbnailUrlInfo.Url, cts.Token);

                var stream = await response.Content.ReadAsStreamAsync();

                var source = await Task.Run(() =>
                {
                    try
                    {
                        var bitm = new BitmapImage
                        {
                            CacheOption   = BitmapCacheOption.OnLoad,
                            CreateOptions = BitmapCreateOptions.IgnoreImageCache
                        };
                        bitm.BeginInit();
                        bitm.StreamSource = stream;
                        bitm.EndInit();
                        bitm.Freeze();
                        stream.Dispose();
                        return(bitm);
                    }
                    catch (Exception ex)
                    {
                        App.Log(ex);
                        return(null);
                    }
                }, cts.Token);

                if (source == null)
                {
                    loadex = new Exception("image source is null");
                }
                else
                {
                    PreviewImage.Source = source;
                }
                await getDetailTask;
            }
            catch (Exception ex)
            {
                loadex = ex;
            }

            if (loadex == null)
            {
                var loadSb = this.Sb("LoadedSb");
                loadSb.Completed += (sender, args) => loadingsb.Stop();
                loadSb.Begin();
                RefreshButton.Visibility = Visibility.Collapsed;
            }
            else
            {
                App.Log(loadex);
                var loadfsb = this.Sb("LoadFailSb");
                loadfsb.Completed += (sender, args) => loadingsb.Stop();
                loadfsb.Begin();
                App.Log($"{ImageItem.ThumbnailUrlInfo.Url} 图片加载失败");
            }

            // Loaded
            ImageLoadEnd?.Invoke(this);
        }