コード例 #1
0
 public int SaveItem(TodoItem item)
 {
     lock (locker) {
             if (item.ID != 0) {
                 database.Update(item);
                 return item.ID;
             } else {
                 return database.Insert(item);
             }
         }
 }
コード例 #2
0
        public TodoListPage()
        {
            Title = "Todo";
            NavigationPage.SetHasNavigationBar (this, true);

            listView = new ListView {
                RowHeight=40,
                ItemTemplate = new DataTemplate (typeof(TodoItemCell))
            };

            // These commented-out lines were used to test the ListView prior to integrating the database
            //			listView.ItemsSource = new string [] { "Buy pears", "Buy oranges", "Buy mangos", "Buy apples", "Buy bananas" };
            //			listView.ItemsSource = new TodoItem [] {
            //				new TodoItem {Name = "Buy pears`"},
            //				new TodoItem {Name = "Buy oranges`", Done=true},
            //				new TodoItem {Name = "Buy mangos`"},
            //				new TodoItem {Name = "Buy apples`", Done=true},
            //				new TodoItem {Name = "Buy bananas`", Done=true}
            //			};

            listView.ItemSelected+= (object sender, SelectedItemChangedEventArgs e) => {
                var todoItem = (TodoItem) e.SelectedItem;
                var todoPage = new TodoItemPage();
                todoPage.BindingContext = todoItem;
                Navigation.PushAsync(todoPage);
            };

            var layout = new StackLayout ();
            layout.Children.Add (listView);
            layout.VerticalOptions = LayoutOptions.FillAndExpand;
            Content = layout;

            ToolbarItem tbi = null;
            if (Device.OS == TargetPlatform.iOS) {
                tbi = new ToolbarItem ("+", null, () => {
                    var todoItem = new TodoItem ();
                    var todoPage = new TodoItemPage ();
                    todoPage.BindingContext = todoItem;
                    Navigation.PushAsync (todoPage);
                }, 0, 0);
            }
            if (Device.OS == TargetPlatform.Android) { // BUG: Android doesn't support the icon being null
                tbi = new ToolbarItem ("+", "plus", () => {
                    var todoItem = new TodoItem ();
                    var todoPage = new TodoItemPage ();
                    todoPage.BindingContext = todoItem;
                    Navigation.PushAsync (todoPage);
                }, 0, 0);
            }

            if (Device.OS == TargetPlatform.WinPhone) {
                tbi = new ToolbarItem ("Add", "add.png", () => {
                    var todoItem = new TodoItem ();
                    var todoPage = new TodoItemPage ();
                    todoPage.BindingContext = todoItem;
                    Navigation.PushAsync (todoPage);
                }, 0, 0);
            }

            ToolbarItems.Add (tbi);

            if (Device.OS == TargetPlatform.iOS ) {
                var tbi2 = new ToolbarItem ("?", null, () => {
                    var todos = App.Database.GetItemsNotDone ();
                    var tospeak = "";
                    foreach (var t in todos)
                        tospeak += t.Name + " ";
                    if (tospeak == "")
                        tospeak = "there are no tasks to do";

                    DependencyService.Get<ITextToSpeech> ().Speak (tospeak);
                }, 0, 0);
                ToolbarItems.Add (tbi2);
            }
        }