Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on this I made custom control which:</p> <ul> <li>can download images asynchronously and get them from cache if image</li> <li>is thread safe</li> <li>was downloaded has dependency property to which you can bind to</li> <li>update images, providing new names in initial feed (don’t forget to maintain cache clean operation, e.g. you can parse your feed and asynchronously delete images with no links in feed)</li> </ul> <p><a href="http://ileonenko.wordpress.com/2012/09/28/local-image-caching-with-custom-control-in-wpf/" rel="nofollow">I made a blog post:</a>, and here's the code:</p> <pre><code>public class CachedImage : Image { static CachedImage() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CachedImage), new FrameworkPropertyMetadata(typeof(CachedImage))); } public readonly static DependencyProperty ImageUrlProperty = DependencyProperty.Register("ImageUrl", typeof(string), typeof(CachedImage), new PropertyMetadata("", ImageUrlPropertyChanged)); public string ImageUrl { get { return (string)GetValue(ImageUrlProperty); } set { SetValue(ImageUrlProperty, value); } } private static readonly object SafeCopy = new object(); private static void ImageUrlPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { var url = (String)e.NewValue; if (String.IsNullOrEmpty(url)) return; var uri = new Uri(url); var localFile = String.Format(Path.Combine(Globals.CacheFolder, uri.Segments[uri.Segments.Length - 1])); var tempFile = String.Format(Path.Combine(Globals.CacheFolder, Guid.NewGuid().ToString())); if (File.Exists(localFile)) { SetSource((CachedImage)obj, localFile); } else { var webClient = new WebClient(); webClient.DownloadFileCompleted += (sender, args) =&gt; { if (args.Error != null) { File.Delete(tempFile); return; } if (File.Exists(localFile)) return; lock (SafeCopy) { File.Move(tempFile, localFile); } SetSource((CachedImage)obj, localFile); }; webClient.DownloadFileAsync(uri, tempFile); } } private static void SetSource(Image inst, String path) { inst.Source = new BitmapImage(new Uri(path)); } } </code></pre> <p>Now you can bind to it:</p> <pre><code>&lt;Cache:CachedImage ImageUrl="{Binding Icon}"/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload