Controlling caching of BitMapImage under WinRT

By default Bitmap Images are cached by WinRT with no intervention required by the developer.  Often times this is a good thing but there are some occasions where this is undesirable.  Although there doesn’t appear to be any obvious control in the API reference I have found that adding a dummy querystring to the BitMapImage source filename reference does the trick and also can offer precise control over the length of caching required (if any) .

Here is how I currently manage caching in a project and limit it to being refreshed once every hour:

In My App.Config I set up a static variable:

public static String CacheOffset = "?Cache=" + ;
System.DateTime.Now.DayOfYear.ToString() + ;

When I populate my local collection I then use something like:

String MyImagePath=”MyBitMapImage.png”+App.CacheOffset;

This way the images are cached for the duration of the users WinRT session and also up to an hour beyond the initial population of my data collection.   Obviously I can vary this duration using different parameters from the current system date and time.  For instance the following would ensure that effectively no caching is carried out between user sessions:

public static String CacheOffset = "?Cache=" + ; System.DateTime.Now.Ticks.ToString() ;

If you wanted to update the image during a session then it would simply be a case of populating your image source using a different ‘virtual cache’ value for CacheOffset.

I suspect there may be better ways to achieve the same objective but for now this works for me.

2 thoughts on “Controlling caching of BitMapImage under WinRT

  1. Pingback: Dot.Blog | Cachez ces images que je ne saurais voir ! (WinRT et le cache image)

  2. Pingback: Windows 8 Developer Links – 2012-05-29 | Dan Rigby

Leave a Reply

Your email address will not be published.