static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { string url = (string)e.NewValue; if (url == null) { return; } AsyncImage img = d as AsyncImage; if (img == null) { return; } img.Source = null; WebClient client = new WebClient(); client.DownloadDataCompleted += OnImageReady; //Muddafukka is slow. ThreadPool.QueueUserWorkItem(a => { try { client.DownloadDataAsync(new Uri(url), img); client.Dispose(); } catch { //TODO: Show no image thing. } }); }
static void OnImageReady(object sender, DownloadDataCompletedEventArgs e) { AsyncImage d = e.UserState as AsyncImage; if (d == null) { return; } if (e.Error != null) { d.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(a => { d.Source = null; d.Visibility = Visibility.Collapsed; return(null); }), null); return; } byte[] data = e.Result; if (data == null) { return; } d.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(a => { MemoryStream stream = new MemoryStream(data); BitmapImage imageSource = new BitmapImage(); imageSource.BeginInit(); imageSource.StreamSource = stream; imageSource.EndInit(); d.Source = imageSource; return(null); }), null); }