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.


~ by Ian on May 27, 2012.

2 Responses to “Controlling caching of BitMapImage under WinRT”

  1. […] object via reflection and get its DisplayName. You can use the following…”Controlling caching of BitMapImage under WinRT (Ian Walker)“By default Bitmap Images are cached by WinRT with no intervention required by […]

  2. […] existe une première feinte publiée par Ian Walkers sur son blog et qui est assez intéressante : visiblement WinRT interprète les URL avant de leur […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: