Articles in this section
Category / Section

How to render DataForm using RealmObject in Xamarin.Forms?

2 mins read

DataForm allows you use Realm database in its DataObject.

This article explains about Realm and how to render DataForm with RealmObject in business data object.

Realm is an object database that stores your objects directly in its database. You can perform the object’s modifications such as deleting, adding, and updating through realm. It uses the model class in DataForm as its data model.

To use RealmObject in DataForm, use the following two steps:

Step 1: Inherit the model class from RealmObject. You can handle your performances over the model class.

public class ContactInfo : RealmObject
    public ContactInfo()
    public string Name { get; set; }
    [Display(Name = "Contact Number")]
    public string ContactNumber { get; set; }
    public string Email { get; set; }
    public string Address { get; set; }
    private DateTime? birthDate;
    [Display(Name = "Birth Date")]
    public DateTime? BirthDate
            return birthDate;
            birthDate = value;
    private DateTime? birthTime;
    [Display(Name = "Birth Time")]
    public DateTime? BirthTime
            return birthTime;
            birthTime = value;


Step 2: Realm mainly depends on Fody weaver and is responsible for turning your RealmObject subclasses into persisted classes. The classes that descend from RealmObjects are processed by the Fody weaver at compilation time. If FodyWeavers.xml is not created automatically, it should be manually created as in the following code snippet for .NET Standard, Android, iOS, and UWP platforms.

<?xml version="1.0" encoding="utf-8" ?>
    <RealmWeaver />



Refer to the following code example for binding the DataObject to dataform.


        <local:ViewModel x:Name="viewModel"/>
        <dataForm:SfDataForm x:Name="dataForm"  DataObject="{Binding Contact}"/>


The following code snippet explains how to create your RealmObject , which must be created only within a write transaction.

public ViewModel()
    contact = new ContactInfo();
    var realm = Realm.GetInstance();
    realm.Write(() =>
        var contactin = realm.Add<ContactInfo>(contact, true);
        contactin.Name = "Rex";
        contactin.Email = "";
        contactin.ContactNumber = "199-1234";
        contactin.Address = "Sout cross street";
        contactin.BirthDate = new DateTime(1996, 04, 13);
        contactin.BirthTime = new DateTime(1996, 04, 13, 12, 12, 12);


While model class is inherited from RealmObject class, the base class public properties are generated as DataForm items , to avoid this you can use AutoGeneratingDataFormItem event to cancel the base class data fields.

dataForm.AutoGeneratingDataFormItem += DataForm_AutoGeneratingDataFormItem;
private void DataForm_AutoGeneratingDataFormItem(object sender, AutoGeneratingDataFormItemEventArgs e)
    if (e.DataFormItem.PropertyInfo.DeclaringType.Name != "ContactInfo")
        e.Cancel = true;


Sample Demo: DataFormRealmObject

Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Please sign in to leave a comment