【发布时间】:2010-11-09 19:11:25
【问题描述】:
我有一个System.Windows.Controls.Image 的实例,我以编程方式设置内容如下:
Uri location = new Uri(uriString);
image.Source = new BitmapImage(location);
有时我知道服务器上的图像发生了变化,我想刷新它,但每次我重复上面的代码都会得到相同的图像。
这似乎是一个缓存问题,但两个明显的解决方案——RequestCacheLevel 和BitmapCacheOption——似乎什么也没做。此代码具有相同的结果:
var cachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)) {
CacheOption = BitmapCacheOption.None
};
image.Source = new BitmapImage(location, cachePolicy);
// Still uses the cached version.
我发现强制刷新的唯一方法是将一次性查询字符串附加到 URI,这似乎可行,但也是一个完整的 hack:
Uri location = new Uri(uriString + "?nonsense=" + new Random().Next());
image.Source = new BitmapImage(location);
// This forces a refresh
如何防止这些图像被缓存和/或强制刷新?
【问题讨论】: