public async void AddItem(View view)
        {
            if (client == null || string.IsNullOrWhiteSpace(textNewToDo.Text))
            {
                return;
            }

            // Create a new item
            var item = new ToDoItemDocDb {
                Text     = textNewToDo.Text,
                Complete = false
            };

            try {
                // Insert the new item into the local store.
                await todoTable.InsertAsync(item);

                // Send changes to the mobile app backend.
                await SyncAsync();

                if (!item.Complete)
                {
                    adapter.Add(item);
                }
            }
            catch (Exception e) {
                CreateAndShowDialog(e, "Error");
            }

            textNewToDo.Text = "";
        }
        public async Task CheckItem(ToDoItemDocDb item)
        {
            if (client == null)
            {
                return;
            }

            // Set the item as completed and update it in the table
            item.Complete = true;
            try {
                // Update the new item in the local store.
                await todoTable.UpdateAsync(item);

                // Send changes to the mobile app backend.
                await SyncAsync();

                if (item.Complete)
                {
                    adapter.Remove(item);
                }
            }
            catch (Exception e) {
                CreateAndShowDialog(e, "Error");
            }
        }
        private async void CheckBoxComplete_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox      cb   = (CheckBox)sender;
            ToDoItemDocDb item = cb.DataContext as ToDoItemDocDb;

            await UpdateCheckedTodoItem(item);
        }
        private async void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            var todoItem = new ToDoItemDocDb {
                Text = TextInput.Text
            };

            TextInput.Text = "";
            await InsertTodoItem(todoItem);
        }
        public async Task CompleteItemAsync(ToDoItemDocDb item)
        {
            try {
                item.Complete = true;
                await todoTable.UpdateAsync(item); // Update todo item in the local database
                await SyncAsync();                 // Send changes to the mobile app backend.


                Items.Remove(item);
            } catch (MobileServiceInvalidOperationException e) {
                Console.Error.WriteLine(@"ERROR {0}", e.Message);
            }
        }
        public async Task InsertTodoItemAsync(ToDoItemDocDb todoItem)
        {
            try {
                await todoTable.InsertAsync(todoItem);  // Insert a new TodoItem into the local database.

#if OFFLINE_SYNC_ENABLED
                await SyncAsync(); // Send changes to the mobile app backend.
#endif

                Items.Add(todoItem);
            } catch (MobileServiceInvalidOperationException e) {
                Console.Error.WriteLine(@"ERROR {0}", e.Message);
            }
        }
        private async Task InsertTodoItem(ToDoItemDocDb todoItem)
        {
            // This code inserts a new TodoItem into the database. After the operation completes
            // and the mobile app backend has assigned an id, the item is added to the CollectionView.
            await todoTable.InsertAsync(todoItem);

            todoItems.Add(todoItem);

            try
            {
                await App.MobileService.SyncContext.PushAsync();
            }
            catch { }
        }
        private async Task UpdateCheckedTodoItem(ToDoItemDocDb item)
        {
            // This code takes a freshly completed TodoItem and updates the database.
            // After the MobileService client responds, the item is removed from the list.
            await todoTable.UpdateAsync(item);

            todoItems.Remove(item);
            ListItems.Focus(Windows.UI.Xaml.FocusState.Unfocused);

            try
            {
                await App.MobileService.SyncContext.PushAsync();
            }
            catch { }
        }
        async partial void OnAdd(UIButton sender)
        {
            if (string.IsNullOrWhiteSpace(itemText.Text))
            {
                return;
            }

            var newItem = new ToDoItemDocDb
            {
                Text     = itemText.Text,
                Complete = false
            };

            await todoService.InsertTodoItemAsync(newItem);

            var index = todoService.Items.FindIndex(item => item.Id == newItem.Id);

            TableView.InsertRows(new[] { NSIndexPath.FromItemSection(index, 0) },
                                 UITableViewRowAnimation.Top);

            itemText.Text = "";
        }
 public ToDoItemDocDbWrapper(ToDoItemDocDb item)
 {
     ToDoItem = item;
 }