Getting started with Image Caching in .NET MAUI
Have you ever wondered how to cache images in your app? .NET MAUI already includes this functionality! In this article, we will quickly learn how to use it and adapt it to our specific needs.
The explanation will be divided into the following points:
š¹Ā Ā How does image caching work?
š¹Ā Ā Learning about its structure
š¹Ā Ā Properties availableĀ
Letās start!
How does image caching work?
In .NET MAUI, downloaded images are cached by default for one day. This functionality is in action whenever you download an image from a URL.
Let's learn about its structure:
Properties available
You can experiment with different values using the properties provided by the UriImageSource class. Letās explore each one of these properties:
š¹ URL: The URI of the image is provided, allowing it to be downloaded for display āĀ It receives a Uri as value.
š¹ CachingEnabled: Determines whether the image cache is enabled or disabled.Ā - It receives a Bool data type and its default value is True.
š¹ CacheValidity: The value entered here will decide how long the image is saved on the local device. Keep in mind that the default value is set with one day as value. ā It receives a TimeSpan as value.
ā If you send a single number, it will be interpreted as the number of days you want the cache to store the data. Since this property is of type TimeSpan, you can format it in XAML and pass a value like this:
Now that we understand the structure and properties, let's dive into the code implementation:
And done!!Ā šĀ From now on, you are ready to works your Images with cache .NET MAUI I hope you like it! šš
<Label Text=āThanks for ready!Ā š ā />Ā Spanish article:
Spanish post:
https://es.askxammy.com/almacenamiento-de-imagenes-en-cache-en-net-maui/
References:
https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/image?view=net-maui-7.0?WT.mc_id=DT-MVP-50033
askxammy.com