|
1 | | -# display-uri-image-listview-xamarin |
2 | | -How to display URI images in Xamarin.Forms ListView (SfListView) |
| 1 | +# How to display URI images in Xamarin.Forms ListView (SfListView) |
| 2 | + |
| 3 | +You can display [URI](https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.uriimagesource?view=xamarin-forms) images in [SfListView](https://help.syncfusion.com/xamarin/listview/overview?) in Xamarin.Forms by loading [Image](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows) in [ItemTemplate](https://help.syncfusion.com/cr/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~ItemTemplate.html?). |
| 4 | + |
| 5 | +You can also refer the following article. |
| 6 | + |
| 7 | +https://www.syncfusion.com/kb/11485/how-to-display-uri-images-in-xamarin-forms-listview-sflistview |
| 8 | + |
| 9 | +**C#** |
| 10 | + |
| 11 | +Defining **ContactImage** property in **Model**. |
| 12 | +``` c# |
| 13 | +namespace ListViewXamarin |
| 14 | +{ |
| 15 | + public class Contacts : INotifyPropertyChanged |
| 16 | + { |
| 17 | + private string image; |
| 18 | + |
| 19 | + public string ContactImage |
| 20 | + { |
| 21 | + get { return this.image; } |
| 22 | + set |
| 23 | + { |
| 24 | + this.image = value; |
| 25 | + this.RaisedOnPropertyChanged("ContactImage"); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public event PropertyChangedEventHandler PropertyChanged; |
| 30 | + |
| 31 | + public void RaisedOnPropertyChanged(string _PropertyName) |
| 32 | + { |
| 33 | + if (PropertyChanged != null) |
| 34 | + { |
| 35 | + PropertyChanged(this, new PropertyChangedEventArgs(_PropertyName)); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | +**C#** |
| 42 | + |
| 43 | +Populating **ContactImage** in **ViewModel** with URI. |
| 44 | +``` c# |
| 45 | +namespace ListViewXamarin |
| 46 | +{ |
| 47 | + public class ContactsViewModel : INotifyPropertyChanged |
| 48 | + { |
| 49 | + |
| 50 | + public ObservableCollection<Contacts> contactsinfo { get; set; } |
| 51 | + |
| 52 | + public void GenerateInfo() |
| 53 | + { |
| 54 | + Random r = new Random(); |
| 55 | + for (int i = 0; i < 40; i++) |
| 56 | + { |
| 57 | + var contact = new Contacts(); |
| 58 | + contact.ContactImage = ""; //Your Url goes here |
| 59 | + contactsinfo.Add(contact); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public event PropertyChangedEventHandler PropertyChanged; |
| 64 | + |
| 65 | + public void OnPropertyChanged(string name) |
| 66 | + { |
| 67 | + if (this.PropertyChanged != null) |
| 68 | + this.PropertyChanged(this, new PropertyChangedEventArgs(name)); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | +**XAML** |
| 74 | + |
| 75 | +Binding **ContactImage** property with Image **Source** property. |
| 76 | +``` xml |
| 77 | +<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" |
| 78 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 79 | + xmlns:local="clr-namespace:ListViewXamarin" |
| 80 | + xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" |
| 81 | + x:Class="ListViewXamarin.MainPage" Padding="{OnPlatform iOS='0,40,0,0'}"> |
| 82 | + |
| 83 | + <ContentPage.BindingContext> |
| 84 | + <local:ContactsViewModel/> |
| 85 | + </ContentPage.BindingContext> |
| 86 | + |
| 87 | + <ContentPage.Content> |
| 88 | + <StackLayout> |
| 89 | + <syncfusion:SfListView x:Name="listView" ItemSpacing="1" AutoFitMode="Height" ItemsSource="{Binding contactsinfo}"> |
| 90 | + <syncfusion:SfListView.ItemTemplate > |
| 91 | + <DataTemplate> |
| 92 | + <ViewCell> |
| 93 | + <ViewCell.View> |
| 94 | + <Grid x:Name="grid" RowSpacing="0"> |
| 95 | + <Grid.RowDefinitions> |
| 96 | + <RowDefinition Height="*" /> |
| 97 | + <RowDefinition Height="1" /> |
| 98 | + </Grid.RowDefinitions> |
| 99 | + <Grid RowSpacing="0"> |
| 100 | + <Grid.ColumnDefinitions> |
| 101 | + <ColumnDefinition Width="70" /> |
| 102 | + <ColumnDefinition Width="*" /> |
| 103 | + </Grid.ColumnDefinitions> |
| 104 | + |
| 105 | + <Frame CornerRadius="2" OutlineColor="Black" Padding="2" Margin="2" HasShadow="True"> |
| 106 | + <Image HeightRequest="95" WidthRequest="95" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFit"> |
| 107 | + <Image.Source> |
| 108 | + <UriImageSource Uri="{Binding ContactImage}" CacheValidity="1" CachingEnabled="true"/> |
| 109 | + </Image.Source> |
| 110 | + </Image> |
| 111 | + </Frame> |
| 112 | + |
| 113 | + <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center"> |
| 114 | + <Grid.RowDefinitions> |
| 115 | + <RowDefinition Height="*" /> |
| 116 | + <RowDefinition Height="*" /> |
| 117 | + </Grid.RowDefinitions> |
| 118 | + <Label Text="{Binding ContactName}"/> |
| 119 | + <Label Grid.Row="1" Grid.Column="0" Text="{Binding ContactNumber}"/> |
| 120 | + </Grid> |
| 121 | + </Grid> |
| 122 | + </Grid> |
| 123 | + </ViewCell.View> |
| 124 | + </ViewCell> |
| 125 | + </DataTemplate> |
| 126 | + </syncfusion:SfListView.ItemTemplate> |
| 127 | + </syncfusion:SfListView> |
| 128 | + </StackLayout> |
| 129 | + </ContentPage.Content> |
| 130 | +</ContentPage> |
| 131 | +``` |
0 commit comments