We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to render ListView using RealmObject in Xamarin.Forms (SfListView)

Platform: Xamarin.Forms |
Control: SfListView |
Published Date: April 24, 2020 |
Last Revised Date: April 24, 2020

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>

View sample in GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile