作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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
如何防止这些图像被缓存和/或强制刷新?
最佳答案
我认为您需要将 BitmapImage 上的 CreateOptions 设置为:
BitmapCreateOptions.IgnoreImageCache
关于.net - WPF Image.Source 缓存过于激进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4137469/
我是一名优秀的程序员,十分优秀!