简体   繁体   中英

Invalidating WinUI/UWP BitmapImage cache

I'm trying to work out a way of invalidating the cache for a 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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