Esempio n. 1
0
        // TODO: Refactor the ListView to be an ObservableCollection.
        //       This should allow the removal of the custom ItemTapped handler.
        //       With an ObservableCollection, we could take advantage of that control's built-in item tap handlers.


        // View Constructor
        public NotesPage()
        {
            // Init
            Title = "Notes App";
            var notesPageViewModel = new NotesPageViewModel(Navigation);

            BindingContext = notesPageViewModel;


            // Add Note Button
            var addButton = new Button
            {
                Text              = "Add Note",
                FontSize          = 20,
                ImageSource       = ImageSource.FromResource("notes.Assets.plus.png"),
                TextColor         = Color.White,
                HorizontalOptions = LayoutOptions.Start,
                WidthRequest      = 160,
                Margin            = new Thickness(10, 10, 10, 0),
                BackgroundColor   = (Color)App.Current.Resources["dctBlue"]
            };

            addButton.SetBinding(Button.CommandProperty, nameof(NotesPageViewModel.AddNoteCommand));


            // Show Done Notes Switch
            var showDoneNotes = new Switch
            {
                IsToggled         = false,
                HorizontalOptions = LayoutOptions.Start
            };

            showDoneNotes.SetBinding(Switch.IsToggledProperty, nameof(NotesPageViewModel.ShowDoneNotes));

            var showDoneNotesBundle = new StackLayout
            {
                Margin   = new Thickness(10, 10, 10, 0),
                Children =
                {
                    new StackLayout
                    {
                        Padding         = new Thickness(5),
                        BackgroundColor = Color.LightGray,
                        Orientation     = StackOrientation.Horizontal,
                        Children        =
                        {
                            new Label
                            {
                                Text     = "Show Done Notes",
                                FontSize = 20
                            },
                            showDoneNotes
                        }
                    }
                }
            };


            // Title
            var titleBundle = new StackLayout
            {
                Margin   = new Thickness(10, 5, 10, 0),
                Children =
                {
                    new BoxView
                    {
                        Margin          = new Thickness(0, 5, 0, 0),
                        BackgroundColor = Color.DarkGray,
                        HeightRequest   = 1,
                        WidthRequest    = 1
                    },
                    new Label
                    {
                        Text           = "Notes:",
                        FontSize       = 20,
                        FontAttributes = FontAttributes.Bold
                    }
                }
            };


            var notesListView = new ListView
            {
                Margin       = new Thickness(20, 5),
                ItemTemplate = new DataTemplate(() =>
                {
                    var label = new Label
                    {
                        FontSize = 20,
                        VerticalTextAlignment = TextAlignment.Center,
                        HorizontalOptions     = LayoutOptions.Start
                    };
                    label.SetBinding(Label.TextProperty, "NoteTitle");

                    var tick = new Image
                    {
                        Source            = ImageSource.FromResource("notes.Assets.check.png"),
                        HorizontalOptions = LayoutOptions.EndAndExpand
                    };
                    tick.SetBinding(VisualElement.IsVisibleProperty, "Done");

                    var stackLayout = new StackLayout
                    {
                        Padding           = new Thickness(5),
                        Margin            = new Thickness(0, 0, 0, 2),
                        BackgroundColor   = Color.LightGray,
                        Orientation       = StackOrientation.Horizontal,
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                        Children          =
                        {
                            label,
                            tick
                        }
                    };

                    return(new ViewCell {
                        View = stackLayout
                    });
                })
            };

            notesListView.SetBinding(ListView.ItemsSourceProperty, nameof(NotesPageViewModel.NotesListViewable));
            notesListView.SetBinding(ListView.SelectedItemProperty, nameof(NotesPageViewModel.SelectedNote));
            notesListView.ItemTapped += (sender, e) => { notesPageViewModel.NoteSelectedCommand.Execute(null); };


            // Load up page content
            Content = new StackLayout
            {
                Children =
                {
                    addButton,
                    showDoneNotesBundle,
                    titleBundle,
                    notesListView
                }
            };
        }
        // View constructor
        public NotesDetailPage(NotesPageViewModel notesPageViewModel)
        {
            // Init
            Title          = "Note Details";
            BindingContext = new NotesDetailPageViewModel(notesPageViewModel);


            // Note Title Entry
            var noteTitleEntry = new Entry
            {
                Placeholder     = "Note Title*",
                FontSize        = 20,
                BackgroundColor = Color.LightGray,
                Margin          = new Thickness(10, 10, 10, 0)
            };

            noteTitleEntry.SetBinding(Entry.TextProperty, nameof(NotesDetailPageViewModel.NoteTitle));


            // Note Text Editor
            var noteTextEditor = new Editor
            {
                Placeholder     = "Enter Note",
                FontSize        = 20,
                BackgroundColor = Color.LightGray,
                Margin          = new Thickness(10, 10, 10, 0),
                HeightRequest   = 120
            };

            noteTextEditor.SetBinding(Editor.TextProperty, nameof(NotesDetailPageViewModel.NoteText));


            // Has Due Date Switch
            var hasDueDateSwitch = new Switch
            {
                IsToggled         = false,
                HorizontalOptions = LayoutOptions.Start
            };

            hasDueDateSwitch.SetBinding(Switch.IsToggledProperty, nameof(NotesDetailPageViewModel.HasDueDate));

            var hasDueDateBundle = new StackLayout
            {
                Margin          = new Thickness(10, 10, 10, 0),
                BackgroundColor = Color.LightGray,
                Orientation     = StackOrientation.Horizontal,
                Children        =
                {
                    new Label
                    {
                        Text     = "Has Due Date?",
                        FontSize = 20,
                        Padding  = new Thickness(3, 8),
                    },
                    hasDueDateSwitch
                }
            };


            // Due Date Picker
            var dueDatePicker = new DatePicker
            {
                FontSize    = 20,
                MinimumDate = new DateTime(2019, 1, 1),
                MaximumDate = new DateTime(2022, 12, 31)
            };

            dueDatePicker.SetBinding(DatePicker.DateProperty, nameof(NotesDetailPageViewModel.DueDate));

            var dueDateBundle = new StackLayout
            {
                Margin          = new Thickness(10, 10, 10, 0),
                BackgroundColor = Color.LightGray,
                Children        =
                {
                    new Label
                    {
                        Text     = "Due Date",
                        FontSize = 20,
                        Padding  = new Thickness(3, 0)
                    },
                    dueDatePicker
                }
            };

            dueDateBundle.SetBinding(StackLayout.IsVisibleProperty, nameof(NotesDetailPageViewModel.HasDueDate));


            // Done Switch
            var doneSwitch = new Switch
            {
                IsToggled         = false,
                HorizontalOptions = LayoutOptions.Start
            };

            doneSwitch.SetBinding(Switch.IsToggledProperty, nameof(NotesDetailPageViewModel.Done));

            var doneBundle = new StackLayout
            {
                Margin          = new Thickness(10, 10, 10, 0),
                BackgroundColor = Color.LightGray,
                Orientation     = StackOrientation.Horizontal,
                Children        =
                {
                    new Label
                    {
                        Text     = "Done",
                        FontSize = 20,
                        Padding  = new Thickness(3, 8),
                    },
                    doneSwitch
                }
            };


            // Action Buttons
            var saveButton = new Button
            {
                Text            = "Save",
                FontSize        = 20,
                ImageSource     = ImageSource.FromResource("notes.Assets.disk.png"),
                TextColor       = Color.White,
                WidthRequest    = 160,
                BackgroundColor = (Color)App.Current.Resources["dctBlue"]
            };

            saveButton.SetBinding(Button.CommandProperty, nameof(NotesDetailPageViewModel.SaveNoteCommand));

            var deleteButton = new Button
            {
                Text            = "Delete",
                FontSize        = 20,
                Margin          = new Thickness(10, 0),
                ImageSource     = ImageSource.FromResource("notes.Assets.trash.png"),
                TextColor       = Color.White,
                WidthRequest    = 160,
                BackgroundColor = (Color)App.Current.Resources["dctBlue"]
            };

            deleteButton.SetBinding(Button.CommandProperty, nameof(NotesDetailPageViewModel.DeleteNoteCommand));

            var actionButtonsBundle = new StackLayout
            {
                Margin      = new Thickness(10),
                Orientation = StackOrientation.Horizontal,
                Children    =
                {
                    saveButton,
                    deleteButton
                }
            };


            // Footnote
            var footnote = new Label
            {
                Text           = "* Required",
                FontSize       = 15,
                FontAttributes = FontAttributes.Italic,
                TextColor      = Color.Gray,
                Margin         = new Thickness(10, 0)
            };


            // Load up page content
            Content = new StackLayout
            {
                Children =
                {
                    noteTitleEntry,
                    noteTextEditor,
                    hasDueDateBundle,
                    dueDateBundle,
                    doneBundle,
                    actionButtonsBundle,
                    footnote
                }
            };
        }