Articles in this section
Category / Section

How to use URI images in .NET MAUI ListView?

3 mins read

You can display URI images in .NET MAUI ListView (SfListView) by loading image in ItemTemplate.

C#

Defining ContactImage property as string type in Model class.

public class Contacts : INotifyPropertyChanged
{
    private string contactImage;
 
    public string ContactImage
    {
        get { return this.contactImage; }
        set
        {
            this.contactImage = value;
            this.RaisedOnPropertyChanged("ContactImage");
        }
    }
}

C#

Set the image URI link to the ContactImage property when populating the items.

public class ContactsViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Contacts> ContactsInfo { get; set; }
 
    public ContactsViewModel()
    {
        ContactsInfo = new ObservableCollection<Contacts>();
        GenerateInfo();
    }
 
    public void GenerateInfo()
    {
        Random r = new Random();
        for (int i = 0; i < 40; i++)
        {
            var contact = new Contacts(CustomerNames[i], r.Next(720, 799).ToString() + " - " + r.Next(3010, 3999).ToString());
            contact.ContactImage = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/User_icon_2.svg/220px-User_icon_2.svg.png"; //Your Url goes here
            ContactsInfo.Add(contact);
        }
    }
}

XAML

Binding the ContactImage property to the Image.Source property as UriImageSource.

<ListView:SfListView x:Name="listView" ItemSize="60" ItemsSource="{Binding ContactsInfo}">
    <ListView:SfListView.ItemTemplate >
        <DataTemplate>
            <Frame Padding="2" Margin="2" HasShadow="False" BorderColor="LightGray">
                <Grid x:Name="grid" RowSpacing="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="70" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Image HeightRequest="50" WidthRequest="50" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Aspect="AspectFit">
                        <Image.Source>
                            <UriImageSource Uri="{Binding ContactImage}" CacheValidity="1" CachingEnabled="true"/>
                        </Image.Source>
                    </Image>
                    <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Label Text="{Binding ContactName}" VerticalOptions="CenterAndExpand"/>
                        <Label Grid.Row="1" Text="{Binding ContactNumber}" VerticalOptions="StartAndExpand"/>
                    </Grid>
                </Grid>
            </Frame>
        </DataTemplate>
    </ListView:SfListView.ItemTemplate>
</ListView:SfListView>

View sample in GitHub

URI image in .NET MAUI SfListView

 

Take a moment to peruse the documentation, to learn more about binding data to SfListView with code.


Conclusion

I hope you enjoyed learning about how to use URI images in .NET MAUI ListView (SfListView).

You can refer to our .NET MAUI ListView feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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