• 2 min read

Xamarin.Forms Image Caching in XAML

A lot of mobile applications have to deal with Images. There are a lot of awesome libraries for iOS and Android that will automatically download, optimize, and cache your images for you from a single line or two of code. Xamarin.Forms actually handles all of this for you automatically when you give the Image’s Source property an Image URL. You can read through the awesome documentation on the Xamarin developer portal. If you browse through this documentation you will see a section that says “Image Caching” and that is interesting. Xamarin.Forms actually handles image caching for you automatically for 1 day. You can enable or disable or extend this time easily in C#:

However, something that isn’t documentated just yet, although I will ping Craig about this, is how to do it in XAML. It is actually extremely simple as you can access all the properties of the Source including the ability to extend image caching. Here is what my image looks like with extending the image cache to 14 days:

Are you using Circle Images? Well you can still do it!

You can always adjust the CacheValidity=“14” as it just creates a new timespan in days for you.

James Montemagno
Author

James Montemagno

James Montemagno is a Principal Lead Program Manager for Developer Community at Microsoft. He has been a .NET developer since 2005, working in a wide range of industries including game development, printer software, and web services. Live, Love, Bike, and Code.

Milli the dog peeking up from the bottom of the page