【问题标题】:WPF Image.Source caching too aggressivelyWPF Image.Source 缓存过于激进
【发布时间】:2010-11-09 19:11:25
【问题描述】:

我有一个System.Windows.Controls.Image 的实例,我以编程方式设置内容如下:

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,这似乎可行,但也是一个完整的 hack:

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

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

【问题讨论】:

    标签: .net wpf caching


    【解决方案1】:

    我认为您需要将 BitmapImage 上的 CreateOptions 设置为:

    BitmapCreateOptions.IgnoreImageCache
    

    【讨论】:

    • 是的,这解决了!呃,为什么有这么多缓存选项??
    • 为我自己节省了大量工作。非常感谢:)
    • 如果你在BeginInit之前设置了这个选项,它将被忽略。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2011-08-07
    • 2015-05-19
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多