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