public TodoListPage()
        {
            Title = "Todo";

            listView = new ListView {
                StyleId = "TodoList"
            };
            listView.ItemTemplate = new DataTemplate(typeof(TodoItemCell));

            listView.ItemSelected += async(sender, e) => {
                if (e.SelectedItem == null)
                {
                    return;                     // ensures we ignore this handler when the selection is just being cleared
                }
                var todoItem = (TodoItem)e.SelectedItem;
                var todoPage = new TodoItemPage();
                todoPage.BindingContext = todoItem;
                await Navigation.PushAsync(todoPage);

                ((ListView)sender).SelectedItem = null;                 // clears the 'selected' background
            };

            // make floating (+) image at bottom of screen
            var tap = new TapGestureRecognizer(async(View obj) => {
                var todoItem            = new TodoItem();
                var todoPage            = new TodoItemPage();
                todoPage.BindingContext = todoItem;

                var b = newImage.Bounds;
                b.Y   = b.Y - 50;

                await newImage.LayoutTo(b, 250, Easing.SinIn);
                b.Y = b.Y + 50;
                await newImage.LayoutTo(b, 250, Easing.SinOut);

                await Navigation.PushAsync(todoPage);
            });

            newImage = new Image {
                Source       = "newitem.png",
                WidthRequest = 40,
                Opacity      = 0.8f,
                StyleId      = "TodoAdd"
            };
            newImage.GestureRecognizers.Add(tap);

            AccessibilityEffect.SetAccessibilityTraits(newImage, AccessibilityTrait.Button);
            AccessibilityEffect.SetAccessibilityLabel(newImage, "Add new item");


            layout = new RelativeLayout();
            layout.Children.Add(listView,
                                xConstraint: Constraint.Constant(0),
                                yConstraint: Constraint.Constant(0),
                                widthConstraint: Constraint.RelativeToParent((parent) => { return(parent.Width); }),
                                heightConstraint: Constraint.RelativeToParent((parent) => { return(parent.Height); }));
            layout.Children.Add(newImage,
                                xConstraint: Constraint.RelativeToParent((parent) =>
            {
                return((parent.Width / 2) - 20);                                // center of image (which is 40 wide)
            }),
                                yConstraint: Constraint.RelativeToParent((parent) =>
            {
                return(parent.Height - 60);
            }));
            Content = layout;


            #region toolbar
            var tbiAdd = new ToolbarItem("+", "plus.png", () => {
                var todoItem            = new TodoItem();
                var todoPage            = new TodoItemPage();
                todoPage.BindingContext = todoItem;
                Navigation.PushAsync(todoPage);
            }, 0, 0);
            tbiAdd.StyleId = "ToolbarAdd";
            //tbiAdd.Order = ToolbarItemOrder.Secondary;
            ToolbarItems.Add(tbiAdd);

            var tbiSpeak = new ToolbarItem("?", "chat.png", () => {
                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("Hello from Xamarin Forms");
            }, 0, 0);
            tbiSpeak.StyleId = "ToolbarSpeak";
            // demonstrate toolbar/optionmenu
            //tbiSpeak.Order = ToolbarItemOrder.Secondary;
            ToolbarItems.Add(tbiSpeak);

            #endregion
        }
Exemplo n.º 2
0
        public TodoItemPage()
        {
            this.SetBinding(ContentPage.TitleProperty, "Name");

            NavigationPage.SetHasNavigationBar(this, true);
            var nameLabel = new Label {
                Text = "Name"
            };
            var nameEntry = new Entry {
                StyleId = "TodoName", Placeholder = "Todo action"
            };

            nameEntry.SetBinding(Entry.TextProperty, "Name");

            var notesLabel = new Label {
                Text = "Notes"
            };
            var notesEntry = new Entry {
                StyleId = "TodoNotes", Placeholder = "More info"
            };

            notesEntry.SetBinding(Entry.TextProperty, "Notes");

            var doneLabel = new Label {
                Text = "Done"
            };
            var doneEntry = new Switch  {
                StyleId = "TodoDone"
            };

            doneEntry.SetBinding(Switch.IsToggledProperty, "Done");


            AccessibilityEffect.SetInAccessibleTree(nameLabel, false);
            AccessibilityEffect.SetAccessibilityHint(nameEntry, "Todo name");

            AccessibilityEffect.SetInAccessibleTree(notesLabel, false);
            AccessibilityEffect.SetAccessibilityHint(notesEntry, "Additional notes");

            AccessibilityEffect.SetInAccessibleTree(doneLabel, false);

            AccessibilityEffect.SetAccessibilityLabel(doneEntry, "whether todo item is done");


            var saveButton = new Button {
                Text            = "Save",
                BackgroundColor = Color.Green,
                BorderRadius    = 0,
                TextColor       = Color.White,
                StyleId         = "TodoSave"
            };

            saveButton.Clicked += (sender, e) => {
                var todoItem = (TodoItem)BindingContext;
                App.Database.SaveItem(todoItem);
                this.Navigation.PopAsync();
            };

            var deleteButton = new Button {
                Text            = "Delete",
                BackgroundColor = Color.Red,
                BorderRadius    = 0,
                TextColor       = Color.White,
                StyleId         = "TodoDelete"
            };

            deleteButton.Clicked += (sender, e) => {
                var todoItem = (TodoItem)BindingContext;
                App.Database.DeleteItem(todoItem.ID);
                this.Navigation.PopAsync();
            };

            var cancelButton = new Button {
                Text            = "Cancel",
                BackgroundColor = Color.Gray,
                BorderRadius    = 0,
                TextColor       = Color.White,
                StyleId         = "TodoCancel"
            };

            cancelButton.Clicked += (sender, e) => {
                var todoItem = (TodoItem)BindingContext;
                this.Navigation.PopAsync();
            };


            var speakButton = new Button {
                Text = "Speak", StyleId = "TodoSpeak"
            };

            speakButton.Clicked += (sender, e) => {
                var todoItem = (TodoItem)BindingContext;
                DependencyService.Get <ITextToSpeech>().Speak(todoItem.Name + " " + todoItem.Notes);
            };

            Content = new StackLayout {
                VerticalOptions = LayoutOptions.StartAndExpand,
                Padding         = new Thickness(20),
                Children        =
                {
                    nameLabel,  nameEntry,
                    notesLabel, notesEntry,
                    doneLabel,  doneEntry,
                    saveButton, deleteButton,cancelButton,
                    speakButton
                }
            };
        }