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); }