How to render ListView using RealmObject in Xamarin.Forms application?
You can render Xamarin.Forms SfListView using the Realm object. Please follow the steps below to use the Realm object.
Step 1: Install the Realm Nuget package to the shared code project.
Step 2: Create a model class by inheriting RealmObject.
namespace ListViewXamarin { public class BookInfo : Realms.RealmObject { public BookInfo() { } public string BookName { get; set; } public string BookDescription { get; set; } public string BookAuthor { get; set; } } }
Step 3: Obtain the realm object in the model class repository using the GetInstance method. Populate items and add them to the Realm object.
namespace ListViewXamarin { public class BookInfoRepository { Realms.Realm realm; public BookInfoRepository() { realm = Realms.Realm.GetInstance(); } internal IEnumerable<BookInfo> GetBookInfo() { realm.Write(() => { for (int i = 0; i < BookNames.Count(); i++) { var book = new BookInfo() { BookName = BookNames[i], BookDescription = BookDescriptions[i], BookAuthor = BookAuthers[i], }; realm.Add(book); } }); return realm.All<BookInfo>().AsRealmCollection(); } } }
Step 4: Create a ViewModel collection as IEnumerable and set the realm collection to the ViewModel collection.
namespace ListViewXamarin { public class ViewModel { private IEnumerable<BookInfo> bookInfo; public ViewModel() { GenerateSource(); } public IEnumerable<BookInfo> BookInfo { get { return bookInfo; } set { this.bookInfo = value; } } private void GenerateSource() { BookInfoRepository bookInfoRepository = new BookInfoRepository(); bookInfo = bookInfoRepository.GetBookInfo(); } } }
Step 5: Bind ViewModel collection to SfListView.ItemsSource and bind the model properties to the ItemTemplate.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ListViewXamarin" xmlns:sync="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" x:Class="ListViewXamarin.MainPage"> <ContentPage.BindingContext> <local:ViewModel /> </ContentPage.BindingContext> <sync:SfListView x:Name="listView" AutoFitMode="Height" ItemsSource="{Binding BookInfo}" SelectionBackgroundColor="#d3d3d3"> <sync:SfListView.ItemTemplate> <DataTemplate> <Grid Padding="0,12,8,0"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="1"/> </Grid.RowDefinitions> <StackLayout Orientation="Vertical" Padding="5,-5,0,0" VerticalOptions="Start" Grid.Row="0"> <Label Text="{Binding BookName}" FontAttributes="Bold" FontSize="16" TextColor="#000000" /> <Label Text="{Binding BookAuthor}" Grid.Row="1" FontSize="14" Opacity=" 0.67" TextColor="#000000" /> <Label Text="{Binding BookDescription}" Opacity=" 0.54" TextColor="#000000" FontSize="13"/> </StackLayout> <BoxView Grid.Row="1" HeightRequest="1" Opacity="0.75" BackgroundColor="#CECECE" /> </Grid> </DataTemplate> </sync:SfListView.ItemTemplate> </sync:SfListView> </ContentPage>
Conclusion
I hope you enjoyed learning about how to render ListView using RealmObject in Xamarin.Forms application.
You can refer to our Xamarin.Forms ListView feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms ListView 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!