2010-11-09 44 views
9

我有System.Windows.Controls.Image一个实例,我编程设置内容,例如:WPF Image.Source缓存过于激进

Uri location = new Uri(uriString); 
image.Source = new BitmapImage(location); 

有时候,我知道在服务器上的形象已经改变了,我想刷新,但每次我重复上述代码时,我都会得到相同的图像。

这似乎是一个缓存问题,但两个明显的解决方案 - RequestCacheLevelBitmapCacheOption - 似乎什么都不做。这段代码有相同的结果:

var cachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)) { 
    CacheOption = BitmapCacheOption.None 
}; 
image.Source = new BitmapImage(location, cachePolicy); 
// Still uses the cached version. 

我发现强制刷新的唯一办法就是追加一个一次性查询字符串的URI,这似乎是工作,但也是一个完整的黑客:

Uri location = new Uri(uriString + "?nonsense=" + new Random().Next()); 
image.Source = new BitmapImage(location); 
// This forces a refresh 

如何防止这些图像被缓存和/或强制刷新?

回答

23

我认为你需要在BitmapImage的的CreateOptions设置为:

BitmapCreateOptions.IgnoreImageCache 
+3

是的,这解决了吧!呃,为什么有这么多的缓存选项? – 2010-11-09 22:45:21

+1

为我自己找到了很多工作。非常感谢:) – 2011-10-20 12:14:02

+1

这解决了我女朋友的问题。谢谢! :) – Musikero31 2012-02-05 16:36:21