简体   繁体   English

使 WinUI/UWP BitmapImage 缓存失效

[英]Invalidating WinUI/UWP BitmapImage cache

I'm trying to work out a way of invalidating the cache for a BitmapImage.我正在尝试找出一种使 BitmapImage 的缓存无效的方法。 My suspicion is that under the covers, this is implemented by something akin to a dictionary of path to memory stream, and when I recreate the BitmapImage with the same path it's just reading the cached stream without noticing that the image on the file system has changed.我怀疑在幕后,这是通过类似于 memory stream 路径字典的东西实现的,当我用相同的路径重新创建BitmapImage时,它只是读取缓存的 stream 而没有注意到文件系统上的图像已经改变.

I know that I can work around this with code like this:我知道我可以用这样的代码解决这个问题:

var bi = new BitmapImage();
if(!initialLoad)
   bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bi.UriSource = new Uri(absolutePath);
return bi;

The problem with the above is that it always ignores the cache and loads it fresh.上面的问题是它总是忽略缓存并重新加载它。 What I'd like is for a way to invalidate the image cache when the image changes, but then keep the image in the cache.我想要的是一种在图像更改时使图像缓存无效但随后将图像保留在缓存中的方法。

The image is being loaded from the file system.正在从文件系统加载图像。

Is that possible?那可能吗?

What I'd like is for a way to invalidate the image cache when the image changes, but then keep the image in the cache.我想要的是一种在图像更改时使图像缓存无效但随后将图像保留在缓存中的方法。

I have to say the answer is no, there are no such options that could directly do that.我不得不说答案是否定的,没有这样的选项可以直接做到这一点。 Currently, the API only provides two options for the image cache - cached or ignored so there is no way to achieve the behavior you want.目前,API 仅提供两种图像缓存选项 -缓存忽略,因此无法实现您想要的行为。 As @Simon Mourier mentioned, the only workaround is that you might need to handle the content change event manually and then change the image by yourself.正如@Simon Mourier 提到的,唯一的解决方法是您可能需要手动处理内容更改事件,然后自行更改图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM