Skip to content

SyncfusionExamples/listview-realm-object-xamarin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

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

You can render Xamarin.Forms SfListView using the Realm object. Please follow the steps below to use the Realm object.

You can also refer the following article.

https://www.syncfusion.com/kb/11445/how-to-render-listview-using-realmobject-in-xamarin-forms-sflistview

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>

About

This repository contains sample about how to render ListView using RealmObject in Xamarin.Forms (SfListView)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages