Xamarin.Forms Projects - Second Edition

By Daniel Hindrikes , Johan Karlsson
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Building Our First Xamarin.Forms App

About this book

Xamarin.Forms is a lightweight cross-platform development toolkit for building apps with a rich user interface. Improved and updated to cover the latest features of Xamarin.Forms, this second edition covers CollectionView and Shell, along with interesting concepts such as augmented reality (AR) and machine learning.

Starting with an introduction to Xamarin and how it works, this book shares tips for choosing the type of development environment you should strive for when planning cross-platform mobile apps. You’ll build your first Xamarin.Forms app and learn how to use Shell to implement the app architecture. The book gradually increases the level of complexity of the projects, guiding you through creating apps ranging from a location tracker and weather map to an AR game and face recognition. As you advance, the book will take you through modern mobile development frameworks such as SQLite, .NET Core Mono, ARKit, and ARCore. You’ll be able to customize your apps for both Android and iOS platforms to achieve native-like performance and speed. The book is filled with engaging examples, so you can grasp essential concepts by writing code instead of reading through endless theory.

By the end of this book, you’ll be ready to develop your own native apps with Xamarin.Forms and its associated technologies, such as .NET Core, Visual Studio 2019, and C#.

Publication date:
June 2020
Publisher
Packt
Pages
504
ISBN
9781839210051

 
Building Our First Xamarin.Forms App

In this chapter, we will create a to-do list app and, in doing so, explore all the bits and pieces of what makes up an app. We will look at creating pages, adding content to pages, navigating between pages, and creating a stunning layout. Well, stunning might be a bit of a stretch, but we will be sure to design the app so that you can tweak it to your needs once it is complete!

The following topics will be covered in this chapter:

  • Setting up the project
  • Persisting data locally on a device
  • Using the repository pattern
  • What MVVM is and why it's a great fit for Xamarin.Forms
  • Using Xamarin.Forms pages (as views) and navigating between them
  • Using Xamarin.Forms controls in XAML
  • Using data binding
  • Using styling in Xamarin.Forms
 

Technical requirements

To complete this project, you need to have Visual Studio installed to your Macintosh (Mac) or PC, as well as the Xamarin components. Refer to Chapter 1, Introduction to Xamarin, for more details on how to set up your environment.

 

An overview of the project

Everyone needs a way of keeping track of things. To kick-start our Xamarin.Forms development learning curve, we've decided that a to-do list app is the best way to get started and to help you keep track of things. A simple, classic win-win scenario.

We will start by creating a project and defining a repository to store the items of a to-do list. We will render these items in list form and allow the user to edit them using a detailed user interface. We will also look at how to store the to-do list items locally on a device through SQLite.NET so that they don't get lost when we exit the app.

The build time for this project is about 2 hours.

 

Beginning the project

It's time to start coding! Before starting, however, make sure you have your development environment set up as described in Chapter 1, Introduction to Xamarin.

This chapter will be a classic File | New | Project chapter, guiding you step by step through the process of creating your first to-do list app. There will be no downloads required whatsoever.

Setting up the project

A Xamarin app can essentially be created by using one of two code-sharing strategies:

  • As a shared project
  • The .NET Standard library

The first choice, a shared project, creates a project that is essentially a linked copy of each file in it. The files exist in one common place and are linked in at build time. This means that we cannot determine the runtime when writing the code and we can only access the APIs that are available on each target platform. It does allow us to use conditional compilations, which can be useful in certain circumstances, but can also be confusing to someone who...

 

Summary

You should now have a good grasp of all the steps involved in creating a Xamarin.Forms app from scratch. We learned about the project structure and the important files in a newly created project. We talked about dependency injection using Autofac and learned the basics of MVVM by creating all the views and the ViewModel classes needed. We also covered data storage in SQLite to persist data on our device in a fast and secure way. Using the knowledge gained from this chapter, you should now be able to create the backbone of any app you like.

The next chapter will focus on creating a richer user experience by creating a match-making application that displays images that you can pan around the screen. We will take a closer look at XAML and how to create custom controls.

About the Authors

  • Daniel Hindrikes

    Daniel Hindrikes is a developer and architect with a passion for developing mobile apps powered by the cloud. Daniel fell in love with Xamarin in the early days of Xamarin when he realized that he could use C# even for iOS and Android apps, and that he could share code with the Windows apps he was also building. But Daniel started to build mobile apps long before that, working on Android apps with Java and even Java ME apps (a long, long time ago). Daniel enjoys sharing his knowledge and can be found speaking at conferences, blogging, or recording the podcast, The Code Behind. Daniel works at the company tretton37 in Sweden and has experience working with both local and global customers.

    Browse publications by this author
  • Johan Karlsson

    Johan Karlsson has been working with Xamarin since the days of MonoTouch and Mono for Android, and it all started with writing a game. He is a full-stack developer, currently focusing on mobile applications using Xamarin, but has in the past worked a lot with ASP.NET MVC, Visual Basic.NET (not proud), and C# in general. Also, he's created a whole bunch of databases in SQL Server over the years. Johan works at tretton37 in Sweden and has about 20 years' experience in the trade of assembling ones and zeros.

    Browse publications by this author
Xamarin.Forms Projects - Second Edition
Unlock this book and the full library FREE for 7 days
Start now