52 Weeks of Xamarin: Week 2 – Starting the project

This is the second week of 52 weeks of Xamarin. To make this all more interesting and practical, we’ll start on building a quasi-real-world application; the iconic To Do List.

The application shown in this and future columns is based on an application I explore in depth in my forthcoming Pluralsight course, Beginning Mobile Development with Xamarin

Step 1 is to open a new project. Choose Cross-platform App/ Blank Xamarin.Forms App, and name it 52ToDo. As you work your way through the configuration wizard be sure that Use Portable Class Library is selected.

I like to use the MVVM (Model-View-ViewModel) pattern wherever I can, and Xamarin.Forms is a great place to do so. In short, MVVM breaks your code up into three groups:

Model – your applications data

View – the display / UI

ViewModel – your program’s logic, the glue between the Model and the View

To facilitate this, add three folders: Model, View and ViewModel. We’ll come back to MVVM quite a bit in coming weeks.

Open 52ToDo.cs and fix the App() method so that it looks like this:

publicApp(){MainPage=newNavigationPage(newCreatePage());}

The goal here is to add the Navigation support provided by Xamarin.Forms and to set the starter page as CreatePage, which we’ve not created yet. Let’s do so…

Right click on the project, and choose Forms/ Forms ContentPage XAML. Name the new form CreatePage. This creates two files, the CreatePage.xaml and CreatePage.xaml.cs pages. In CreatePage.xaml we’ll be using a number of Views: Label and Entry, Button and Date and Time pickers. Their names are self-explanatory. All the rest of the code is StackLayouts. Here is the code, study it in some detail and see if you can anticipate what the page will look like:

That’s a lot of code, but it is pretty straight forward. Sketch out on a piece of paper what you think it will look like. Take into account the various Stack Layout objects. Now run the application. Does it look as you expected? Make sure you are comfortable with the appearance and how it relates to the XAML before going further.

Click on a date or a time. Notice that we get the entire interaction with the date and time pickers for free. We’ll see later in this set of blog posts that things are not quite so easy in Android, and even a bit more difficult in iOS. But that is for another day.

Notice also that the buttons, Save, Cancel and Review don’t work. That is because we’ve not yet added events and event handlers, which we will do next week.

About Jesse Liberty

Jesse Liberty is a Senior Consultant with Wintellect, with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight & LinkedIn Learning courses, and has been a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.