How to use URI images in .NET MAUI ListView?
You can display URI images in .NET MAUI ListView (SfListView) by loading the image in the ItemTemplate.
C#
Defining the ContactImage property as a 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
Bind 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>
Download the complete sample on GitHub.
Conclusion
I hope you enjoyed learning how to use URI images in .NET MAUI ListView (SfListView).
You can refer to our .NET MAUI ListView feature tour page to learn about its other groundbreaking feature representations. Explore our documentation to understand how to create and manipulate data.
For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. Contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!