How to import and export data objects in Xamarin Dataform?
Xamarin DataForm allows you to bind data from local database using SQLite. To achieve this, follow the below steps:
Step 1: Install the sqlite-net-pcl NuGet package in your shared code project.
Step 2: Configure app constants to provide common configurations. The following code example explains about app constants to provide the common configurations.
public static class Constants { public const string DatabaseFilename = "SampleSQLites.db3"; public const SQLite.SQLiteOpenFlags Flags = SQLite.SQLiteOpenFlags.ReadWrite | SQLite.SQLiteOpenFlags.Create | SQLite.SQLiteOpenFlags.SharedCache; public static string DatabasePath { get { var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); return Path.Combine(basePath, DatabaseFilename); } } }
Step 3: Create a database access class and initialize the data manipulation methods. Refer to the following code to create a database access class and initialize the data manipulation methods.
public class DataFormDataBase { static readonly Lazy<SQLiteAsyncConnection> lazyInitializer = new Lazy<SQLiteAsyncConnection>(() => { return new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags); }); static SQLiteAsyncConnection Database => lazyInitializer.Value; static bool initialized = false; public DataFormDataBase() { InitializeAsync().SafeFireAndForget(false); } async Task InitializeAsync() { if (!initialized) { if (!Database.TableMappings.Any(m => m.MappedType.Name == typeof(ContactsInfo).Name)) { await Database.CreateTablesAsync(CreateFlags.None, typeof(ContactsInfo)).ConfigureAwait(false); initialized = true; } } } public Task<List<ContactsInfo>> GetItemsAsync() { return Database.Table<ContactsInfo>().ToListAsync(); } public Task<ContactsInfo> GetItemAsync(string name) { return Database.Table<ContactsInfo>().Where(i => i.Name == name).FirstOrDefaultAsync(); } public Task<int> SaveItemAsync(ContactsInfo item) { return Database.InsertAsync(item); } public Task<int> DeleteItemAsync(ContactsInfo item) { return Database.DeleteAsync(item); } }
You can refer to the following link to create the SQLite connection.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/databases
Step 4: Populate the data into the database. The following code explains how to populate the data into the database.
public DataFormViewModel() { ContactsInfo contact = new ContactsInfo(); contact.Name = "Jhon"; contact.ContactNumber = 12345; contact.Email = "abc@mail.com"; contact.BirthDate = new DateTime(1990, 01, 01); App.Database.SaveItemAsync(contact); … }
Step 5: Get the data from the database. Refer to the following code to get data from the database and store it in a property to bind with DataObject.
public DataFormViewModel() { … this.GetDataModel(); } /// <summary> /// Gets the data from database /// </summary> private async void GetDataModel() { var dataObject = await App.Database.GetItemsAsync(); this.Contact = dataObject.FirstOrDefault(); }
Step 6: Save the input values into the database after editing the form.
/// <summary> /// Gets input values from DataForm and save into the database /// </summary> /// <param name="dataForm"></param> private void SaveInputValues(SfDataForm dataForm) { var dataObject = dataForm.DataObject as ContactsInfo; App.Database.SaveItemAsync(dataObject); }
Download the sample from GitHub.
Conclusion
I hope you enjoyed learning about how to import and export data objects in Xamarin Dataform.
You can refer to our Xamarin.Forms DataForm feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!