Blog

Building a Xamarin ToDo List with Backendless

by on February 26, 2019

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.
Backendless with Xamarin

  1. Download and install Xamarin studio.
  2. Download the ToDo sample.
  3. Open the sample with Visual Studio and run it:
    Xamarin ToDo App in Visual Studio
    Now we have a project with SQLite storage.
  4. Go to Backendless Console and create an application for the sample. If you don’t have a Backendless account, you can create a free one here.
  5. Add the Nuget Backendless package:
    Add Nuget Packages in Backendless
  6. Initialize Backendless with your application id and API key:
    App ID and API Key in Backendless
    I have chosen to use the Windows Phone API key, but the name of API key only matters for analytics purposes, so you can choose any API key except the Code Runner API key. (That API key has additional privileges, such as assigning roles to users.)
  7. Go to the App.cs file and init the app in the OnStart method:

    protected override void OnStart()
    {
         BackendlessAPI.Backendless.InitApp("B6E34B32-3AA1-CE73-FF9D-A7B0BF409500", "A0E06CD2-6AF1-719E-FFAC-E5C58027E500");
    }
  8. Then we should change the TodoItem.cs file:

    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; }
       }
    }
  9. And finally, change TodoItemDatabase.cs to use Backendless instead of SQLite:

    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.

Leave a Reply