//View public ListViewDemoPage(ContactRepository database) { _database = database; //Heading var heading = new Label { Text = "Contacts", FontSize = 30, HorizontalOptions = LayoutOptions.Start, FontAttributes = FontAttributes.Bold | FontAttributes.Italic }; //Setup ListView var contacts = _database.GetContacts(); _app.ContactList = new ListView { //To fit custom ViewCell RowHeight = 96, ItemsSource = contacts.OrderBy(x => x.LastName).ToList(), ItemTemplate = new DataTemplate(typeof (ContactCell)) }; //Setup Bindings //_app.ContactList.ItemTemplate.SetBinding(TextCell.TextProperty, "FullName"); //_app.ContactList.ItemTemplate.SetBinding(TextCell.DetailProperty, "Type"); //Build Page var main = new StackLayout { Children = { heading, _app.ContactList } }; Content = main; }
//View public XFormsMenuPage() { //Create Database Object var database = new ContactRepository(); Title = "Forms Menu"; var pageCommand = new Command<Type>(async pageType => { var page = (Page) Activator.CreateInstance(pageType); await Navigation.PushAsync(page); }); #region TableView Content Content = new TableView { Intent = TableIntent.Menu, Root = new TableRoot { new TableSection("Views for Presentation") { new TextCell { Text = "BoxView, Images, and Webpages", Command = pageCommand, CommandParameter = typeof (ViewSourcesDemoPage) }, new TextCell { Text = "Label", Command = pageCommand, CommandParameter = typeof (LabelPage) } }, new TableSection("Views that Initiate Commands"), new TableSection("Views for Common Data Types"), new TableSection("Views for Editing Text"), new TableSection("Views to Indicate Activity"), new TableSection("Views that Display Collections"), new TableSection("Layouts with Single Content") { new TextCell { Text = "ScrollView", Command = pageCommand, CommandParameter = typeof (ScrollViewDemoPage) } }, new TableSection("Layouts with Multiple Children") { new TextCell { Text = "StackLayout", Command = pageCommand, CommandParameter = typeof (StackLayoutDemoPage) }, new TextCell { Text = "ListView", Command = new Command(async() => await Navigation.PushAsync(new ListViewDemoPage(database))) } }, new TableSection("Pages") { new TextCell { Text = "ContentPage", Command = pageCommand, CommandParameter = typeof (ContentDemoPage) }, new TextCell { Text = "NavigationPage", Command = pageCommand, CommandParameter = typeof (NavigationDemoPage) }, new TextCell { Text = "TabbedPage", Command = pageCommand, CommandParameter = typeof (TabbedPageDemo) }, new TextCell { Text = "MasterDetailPage", Command = pageCommand, CommandParameter = typeof (MasterDetailDemoPage) } }, new TableSection("Class Project Apps") { new TextCell { Text = "StopWatch App", Command = pageCommand, CommandParameter = typeof (StopWatchApp) }, new TextCell { Text = "Contact List App", Command = new Command(async () => await Navigation.PushAsync(new ContactListApp(database))) } } } }; #endregion }
//View public ContactEditPage(ContactRepository database) { //Initialize _database = database; //Create EntryCells var firstNameCell = new EntryCell {Label = "First Name:"}; var lastNameCell = new EntryCell {Label = "Last Name:"}; var typeCell = new EntryCell {Label = "Contact Type:"}; var dateCell = new EntryCell { Label = "Date of Birth:"}; //Set DataBinding firstNameCell.SetBinding(EntryCell.TextProperty, "FirstName"); lastNameCell.SetBinding(EntryCell.TextProperty, "LastName"); typeCell.SetBinding(EntryCell.TextProperty, "Type"); dateCell.SetBinding(EntryCell.TextProperty, "DateOfBirth"); //Save Btn var saveBtn = new Button { Text = " Save Contact ", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof (Button)), HorizontalOptions = LayoutOptions.CenterAndExpand }; saveBtn.Clicked += async (sender, args) => { //Validate Text Fields if (firstNameCell.Text == string.Empty || lastNameCell.Text == string.Empty || typeCell.Text == string.Empty || dateCell.Text == string.Empty) { await DisplayAlert ("Warning", "Please Fill In All Fields", "OK"); } else { //Saves Changes to Database var contactItem = (Contact) BindingContext; _database.SaveContact(contactItem); await Navigation.PopAsync(); SortContacts(); } }; //Delete Btn var deleteBtn = new Button { Text = " Delete Contact ", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof (Button)), HorizontalOptions = LayoutOptions.CenterAndExpand }; deleteBtn.Clicked += async (sender, e) => { //Deletes Contact from Database var contactItem = (Contact) BindingContext; _database.DeleteContact(contactItem); await Navigation.PopAsync(); SortContacts(); }; //Cancel Btn var cancelBtn = new Button { Text = " Cancel ", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof (Button)), HorizontalOptions = LayoutOptions.CenterAndExpand }; cancelBtn.Clicked += async (sender, args) => { await Navigation.PopAsync(); }; //Create TableView var tableView = new TableView { HeightRequest = 275, Intent = TableIntent.Form, Root = new TableRoot { new TableSection("Edit/Delete a Contact") { firstNameCell, lastNameCell, dateCell, typeCell } } }; //Build Button Layout var btnLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = {saveBtn, deleteBtn, cancelBtn} }; //Build Main Page var main = new StackLayout { VerticalOptions = LayoutOptions.Start, Children = {tableView, btnLayout} }; Content = main; }
//View public ContactListApp(ContactRepository database) { //Initialize _database = database; //Heading var heading = new Label { Text = "Contact List", FontSize = 50, HorizontalOptions = LayoutOptions.Start, FontAttributes = FontAttributes.Bold | FontAttributes.Italic }; //Buttons var contactBtn = new Button { Text = "Add Contact", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof (Button)), BorderWidth = 1, HorizontalOptions = LayoutOptions.EndAndExpand }; //ButtonEventHandler contactBtn.Clicked += async (sender, args) => { await Navigation .PushAsync(new ContactAddPage(this, database)); }; //Setup ListView var contacts = _database.GetContacts(); _app.ContactList = new ListView { //To fit custom ViewCell RowHeight = 96, ItemsSource = contacts.OrderBy(x => x.LastName).ToList(), ItemTemplate = new DataTemplate(typeof (ContactCell)) }; //Set Bindings (Bindings set within ContactCell for this project) //_app.ContactList.ItemTemplate.SetBinding(ContactCell.FirstNameProperty, "FirstName"); //_app.ContactList.ItemTemplate.SetBinding(ContactCell.LastNameProperty, "FirstName"); //_app.ContactList.ItemTemplate.SetBinding(ContactCell.TypeProperty, "Type"); //_app.ContactList.ItemTemplate.SetBinding(ContactCell.DateProperty, "DateOfBirth"); //ItemSelected Event Handler _app.ContactList.ItemSelected += async (sender, e) => { var selectedContact = (Contact) e.SelectedItem; var editPage = new ContactEditPage(database) { BindingContext = selectedContact }; if (selectedContact == null) return; await Navigation.PushAsync(editPage); _app.ContactList.SelectedItem = null; }; //Heading Layout var headingLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { heading, contactBtn } }; //Build Main Page var main = new StackLayout { Children = { headingLayout, _app.ContactList } }; Content = main; }
//View public ContactAddPage(ContactListApp parentPage, ContactRepository database) { _parentPage = parentPage; _database = database; //EntryCells var firstNameCell = new EntryCell {Label = "First Name:"}; var lastNameCell = new EntryCell {Label = "Last Name:"}; var typeCell = new EntryCell {Label = "Contact Type:"}; var dateCell = new EntryCell { Label = "Date of Birth:" }; //Save Btn var saveBtn = new Button { Text = " Save Contact ", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof (Button)), HorizontalOptions = LayoutOptions.CenterAndExpand }; saveBtn.Clicked += async (sender, e) => { var fName = firstNameCell.Text; var lName = lastNameCell.Text; var type = typeCell.Text; var date = dateCell.Text; //Validate Cells if (fName != null || lName != null || type != null || date != null) { _database.AddContact(fName, lName, date, type ); _parentPage.SortContacts(); //Clear Cells firstNameCell.Text = string.Empty; lastNameCell.Text = string.Empty; typeCell.Text = string.Empty; dateCell.Text = string.Empty; } else { await DisplayAlert ("Warning", "Please Fill In All Fields", "OK"); } }; //Return Btn var returnBtn = new Button { Text = " Return ", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof (Button)), HorizontalOptions = LayoutOptions.CenterAndExpand }; returnBtn.Clicked += async (sender, args) => { await Navigation.PopAsync(); }; //Create TableView var tableView = new TableView { HeightRequest = 275, Intent = TableIntent.Form, Root = new TableRoot { new TableSection("Add a New Contact") { firstNameCell, lastNameCell, dateCell, typeCell } } }; //Build Button Layout var btnLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = {saveBtn, returnBtn} }; //Build Main Page var main = new StackLayout { VerticalOptions = LayoutOptions.Start, Children = {tableView, btnLayout} }; Content = main; }