The new entry page of the app will give the user a way to add a new log entry by presenting a series of fields to collect the log entry details. There are several ways to build a form to collect data in Xamarin.Forms. You can simply use a StackLayout and present a stack of Label and Entry controls on the screen, or you can also use a TableView with various types of ViewCell elements. In most cases, a TableView will give you a very nice default, platform-specific look and feel. However, if your design calls for a more customized aesthetic, you might be better off leveraging the other layout options available in Xamarin.Forms. For the purpose of this app, we will use a TableView.
There are some key data points we need to collect when our users log new entries with the app, such as title, location, date, rating, and notes. For now, we will use a regular EntryCell element for each of these fields. We will update, customize, and add things to these fields later in this book. For example, we will wire the location fields to a geolocation service that will automatically determine the location. We will also update the date field to use an actual platform-specific date picker control. For now, we will just focus on building the basic app shell.
In order to create the new entry page that contains a TableView, perform the following steps:
- First, add a new Xamarin.Forms XAML ContentPage to the core project and name it NewEntryPage.
- Update the new entry page using the following XAML to build the TableView that will represent the data entry form on the page:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TripLog.NewEntryPage"
Title="New Entry">
<ContentPage.Content>
<TableView Intent="Form">
<TableView.Root>
<TableSection>
<EntryCell Label="Title" />
<EntryCell Label="Latitude"
Keyboard="Numeric" />
<EntryCell Label="Longitude"
Keyboard="Numeric" />
<EntryCell Label="Date" />
<EntryCell Label="Rating"
Keyboard="Numeric" />
<EntryCell Label="Notes" />
</TableSection>
</TableView.Root>
</TableView>
</ContentPage.Content>
</ContentPage>
Now that we have created the new entry page, we need to add a way for users to get to this new screen from the main page. We will do this by adding a New button to the main page's toolbar. In Xamarin.Forms, this is accomplished by adding a ToolbarItem to the ContentPage.ToolbarItems collection and wiring up the ToolbarItem.Clicked event to navigate to the new entry page, as shown in the following XAML:
<!-- MainPage.xaml -->
<ContentPage>
<ContentPage.ToolbarItems>
<ToolbarItem Text="New" Clicked="New_Clicked" />
</ContentPage.ToolbarItems>
</ContentPage>
// MainPage.xaml.cs
public partial class MainPage : ContentPage
{
// ...
void New_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new NewEntryPage());
}
}
In Chapter 3, Navigation, we will build a custom service to handle navigation between pages and will replace the Clicked event with a data-bound ICommand ViewModel property, but for now, we will use the default Xamarin.Forms navigation mechanism.
When we run the app, we will see a New button on the toolbar of the main page. Clicking on the New button should bring us to the new entry page, as shown in the following screenshot:
We will need to add a save button to the new entry page toolbar so that we can save new items. For now, this button will just be a placeholder in the UI that we will bind an ICommand to in Chapter 2, MVVM and Data Binding. The save button will be added to the new entry page toolbar in the same way the New button was added to the main page toolbar. Update the XAML in NewEntryPage.xaml to include a new ToolbarItem, as shown in the following code:
<ContentPage>
<ContentPage.ToolbarItems>
<ToolbarItem Text="Save" />
</ContentPage.ToolbarItems>
<!-- ... -->
</ContentPage>
When we run the app again and navigate to the new entry page, we should now see the Save button on the toolbar, as shown in the following screenshot: