Articles in this section
Category / Section

How to bind StaticResource to ImageSource in Xamarin.Forms AvatarView

4 mins read

In Xamarin.Forms SfAvatarView, you can set the key for ImageSource by using ResourceDictionary and bind image from ViewModel to ImageSource.

An ImageSource instance, can be either File, Uri or Resource, which sets the image to display.

ImageSource instances can be obtained by using any static methods for each type:

 

Local images

Image files can be added to each application project and referenced from Xamarin.Forms shared code. 

  • iOS - Images were typically placed in the Resources folder with Build Action: BundleResource.
  • Android - Place images in the Resources/drawable directory with Build Action: AndroidResource. High- and low-DPI versions of an image can also be supplied (in appropriately named Resources sub-directories such as drawable-ldpidrawable-hdpi, and drawable-xhdpi).
  • Universal Windows Platform (UWP) - By default, images should be placed in the application's root directory with Build Action: Content.

 
Following steps describes how to use an image in Xamarin.Forms SfAvatarView

Step 1: Initialize the Xamarin.Forms SfAvatarView in XAML or code behind with the properties required.

Step 2: Add ViewModel class to show the images in Xamarin.Form.SfAvatarView.

Binding image from Static resource

XAML

  <ContentPage.Resources>
        <ResourceDictionary>
            <x:String x:Key="avatarImage">Avatar.png</x:String>
        </ResourceDictionary>
       
    </ContentPage.Resources>
    
    <Grid>
        <avatarView:SfAvatarView ContentType="Custom"
                           ImageSource="{StaticResource avatarImage}"
                           VerticalOptions="Center"
                           HorizontalOptions="Center"   
                           HeightRequest="50"
                           CornerRadius="25"
                           WidthRequest="50" />
    </Grid>

 

 

Binding image from ViewModel

ViewModel

  public class AvatarViewModel : INotifyPropertyChanged
    {
 
        private ImageSource avatarImage = "Avatar.png";
        public ImageSource AvatarImage
        {
            get { return avatarImage; }
            set { avatarImage = value; }
        }
    }

XAML

<ContentPage.BindingContext>
        <local:AvatarViewModel/>
    </ContentPage.BindingContext>
 
    <Grid>
        <avatarView:SfAvatarView ContentType="Custom"
                           ImageSource="{Binding AvatarImage}"
                           VerticalOptions="Center"
                           HorizontalOptions="Center"   
                           HeightRequest="50"
                           CornerRadius="25"
                           WidthRequest="50" />
    </Grid>

Sample
https://github.com/SyncfusionExamples/how-to-bind-staticresource-to-imagesource-in-xamarin.forms-sfavatarview

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied