WPF Image.Source缓存太积极 [英] WPF Image.Source caching too aggressively
问题描述
我有 System.Windows.Controls.Image
的实例,我编程设置内容,例如:
I have an instance of System.Windows.Controls.Image
, and I set the contents programmatically as such:
Uri location = new Uri(uriString);
image.Source = new BitmapImage(location);
有时候,我知道在服务器上的形象已经改变了,我想刷新,但每次我重复上述code,我得到了相同的图像。
Sometimes I know that the image on the server has changed and I want to refresh it, but every time I repeat the above code I get the same image.
这似乎是一个缓存的问题,但两个明显的解决方案 - RequestCacheLevel
和 BitmapCacheOption
- 似乎做什么都没有。这code具有相同的结果:
This seems to be a caching problem, but the two obvious solutions — RequestCacheLevel
and BitmapCacheOption
— seem to do nothing. This code has the same result:
var cachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)) {
CacheOption = BitmapCacheOption.None
};
image.Source = new BitmapImage(location, cachePolicy);
// Still uses the cached version.
我发现强制刷新的唯一方法是追加一次性查询字符串的URI,这似乎是工作,更是一个完整的黑客:
The only way I've found to force a refresh is to append a throwaway query string to the URI, which seems to work, but also is a complete hack:
Uri location = new Uri(uriString + "?nonsense=" + new Random().Next());
image.Source = new BitmapImage(location);
// This forces a refresh
我怎样才能prevent从这些图像被缓存,和/或强制刷新?
How can I prevent these images from being cached, and/or force a refresh?
推荐答案
我想你需要设置上的BitmapImage的CreateOptions为:
I think you need to set the CreateOptions on the BitmapImage to:
BitmapCreateOptions.IgnoreImageCache
这篇关于WPF Image.Source缓存太积极的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!