Giter Site home page Giter Site logo

syncfusionexamples / listview-realm-object-xamarin Goto Github PK

View Code? Open in Web Editor NEW
0.0 5.0 0.0 527 KB

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

C# 100.00%
listview sflistview xamarin xamarin-forms realm-object

listview-realm-object-xamarin's Introduction

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>

listview-realm-object-xamarin's People

Contributors

jayaleshwari avatar sarubala20 avatar vinothkumar-ganesan avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.