We like to think of Backendless as being frontend agnostic. In other words, you can use Backendless for your backend almost regardless of how you build your application’s frontend. As such, it is very easy to use Backendless with Xamarin, an increasingly popular open-source native app builder by Microsoft. You can try out Xamarin for building apps for free with the Community edition of Visual Studio from Microsoft.
In this post, we’re going to create a simple example based on the Xamarin ToDo list sample provided by Xamarin.
protected override void OnStart() { BackendlessAPI.Backendless.InitApp("B6E34B32-3AA1-CE73-FF9D-A7B0BF409500", "A0E06CD2-6AF1-719E-FFAC-E5C58027E500"); }
using System; using SQLite; namespace Todo { public class TodoItem { public string objectID { get; set; } public string Name { get; set; } public strong Notes { get; set; } public bool Done { get; set; } } }
using System; using System.Collections.Generic; using System.Threading.Tasks; using BackendlessAPI.Data; using BackendlessAPI.Persistence; namespace Todo { public class TodoItemDatabase { private IDataStore<TodoItem> dataStore = BackendlessAPI.Backendless.Data.Of<TodoItem>(); public TodoItemDatabase(string dbPath) { } public Task<IList<TodoItem>> GetItemsAsync() { var task = BackendlessAPI.Backendless.Data.Of<TodoItem>().FindAsync(DataQueryBuilder.Create()); return task.ContinueWith<IList<TodoItem>>((resultTask) => { //if there is no table with name TodoItem, an exception occurs: "Table not found by name 'TodoItem'. //Make sure the client class referenced in the API call has the same literal name as the table in //Backendless console'" if ( resultTask.Exception != null ) return new List<TodoItem>(); return resultTask.Result; }); } public Task<IList<TodoItem>> GetItemsNotDoneAsync() { DataQueryBuilder dataQueryBuilder = DataQueryBuilder.Create(); dataQueryBuilder.SetWhereClause("Done = 0"); return dataStore.FindAsync(dataQueryBuilder); } public Task<TodoItem> GetItemAsync(String objectId) { return dataStore.FindByIdAsync(objectId); } public Task<TodoItem> SaveItemAsync(TodoItem item) { return dataStore.SaveAsync(item); } public Task<long> DeleteItemAsync(TodoItem item) { return dataStore.RemoveAsync(item); } } }
That’s all! Now your basic ToDo sample app works with the Backendless database.