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() + ;
System.DateTime.Now.Hour.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.

Advertisements

2 thoughts on “Controlling caching of BitMapImage under WinRT

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

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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