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 DataForm using RealmObject in Xamarin.Forms?

Platform: Xamarin.Forms |
Control: SfDataForm |
Published Date: March 29, 2019 |
Last Revised Date: April 9, 2019

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")]
    [DataType(DataType.Date)]
    public DateTime? BirthDate
    {
        get
        {
            return birthDate;
        }
        set
        {
            birthDate = value;
        }
    }
    private DateTime? birthTime;
    [DataType(DataType.Time)]
    [Display(Name = "Birth Time")]
    public DateTime? BirthTime
    {
        get
        {
            return birthTime;
        }
        set
        {
            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" ?>
<Weavers>
    <RealmWeaver />
</Weavers>

 

 

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

 

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

 

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 = "rex@hotmail.com";
        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

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Zimmermann
Jun 20, 2019

Hi

The solution does not work because the property changed event save the data to the realm database. But the database is no in write mode an exception gets raised.

How is it possible to bind the realm object direct to the dataform and edit values?

Regards, Bruno

Reply

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
Live Chat Icon