Introduction
At this stage, you know the simple way to load an image from a URL. In this article, you are going to learn,
- Different ways to add images to your application.
- How to control Caching Up images downloaded from the internet.
- How to show an activity indicator while the image is downloading.
People with basic knowledge of C# and Xamarin.
ToolsVisual Studio with Xamarin Installed.
Image Sources
In Xamarin, we have two different types of images. We have images that are platform independent, like background images. Also, we have images that are specific to each platform, like icon and splash screens.
To include platform dependent images to our application, we have two options. We can download them using the URI (Unified Resource Identifier). Or, we can embed them in a Portable Class Library (PCL). If you want to work with icons for buttons, splash screens etc., you need to include different images in each application project.
Image Source Using URI
In the picture below, I have got an image element with a source set to its URI. “1920/1080” is the dimension of the image. “/sports” is the category and “/7” is the identifier for the image. So, you will always get the same image.
Code
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:PracApp1"
- x:Class="PracApp1.MainPage"
- x:Name="ContentPage1">
- <Image Source="http://lorempixel.com/1920/1080/sports/7/"></Image>
- </ContentPage>
Create a new UriImageSource directly, set the URI, and store the result into the imageSource variable. The UriImageSource object has a couple of properties that you might be interested in. One of them is CachingEnable. It’s a Boolean and by default, it's true. When using XAML to add an image to your application, we set the image source to a Uri. The XAML parser internally creates a UriImageSource and as caching is enabled by default, that image is cached for the next 24 hours. Which means if you terminate your application and launch it again, that image is loaded from the cache.
But if sometimes, you want to disable caching, for example, you want to download a user’s profile image that can change. In those cases, you cannot use Xaml. Instead, we have to go to the code behind, directly create a UriImageSource object and set CachingEnabled to false. Another property is CacheValidity which is a TimeSpan object. You can set it to time for which you want to cache the image.
XAML Code
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:PracApp1"
- x:Class="PracApp1.MainPage"
- x:Name="ContentPage1">
- <Image x:Name="image"></Image>
- </ContentPage>
C# Code
- public MainPage()
- {
- InitializeComponent();
- var imageSource = new UriImageSource { Uri = new Uri("http://lorempixel.com/1920/1080/sports/7/") };
- imageSource.CachingEnabled = false;
- imageSource.CacheValidity = TimeSpan.FromHours(1);
- image.Source = imageSource;
- }





Join the conversation! Your thoughts help the community grow.