Book Image

Mastering Xamarin.Forms

By : Ed Snider
Book Image

Mastering Xamarin.Forms

By: Ed Snider

Overview of this book

Discover how to extend and build upon the components of the Xamarin.Forms toolkit to develop an effective, robust mobile app architecture. Starting with an app built with the basics of the Xamarin.Forms toolkit, we’ll go step by step through several advanced topics to create a solution architecture rich with the benefits of good design patterns and best practices. We’ll start by introducing a core separation between the app’s user interface and the app’s business logic by applying the MVVM pattern and data binding. Discover how to extend and build upon the components of the Xamarin.Forms toolkit to develop an effective, robust mobile app architecture. Starting with an app built with the basics of the Xamarin.Forms toolkit, we’ll go step by step through several advanced topics to create a solution architecture rich with the benefits of good design patterns and best practices. We’ll start by introducing a core separation between the app’s user interface and the app’s business logic by applying the MVVM pattern and data binding. Then we will focus on building out a layer of plugin-like services that handle platform-specific utilities such as navigation, geo-location, and the camera, as well as how to use these services with inversion of control and dependency injection. Next we’ll connect the app to a live web-based API and set up offline synchronization. Then, we’ll dive into testing the app—both the app logic through unit tests and the user interface using Xamarin’s UITest framework. Finally, we’ll integrate Xamarin Insights for monitoring usage and bugs to gain a proactive edge on app quality.
Table of Contents (17 chapters)

Preface

Xamarin released the Xamarin.Forms toolkit in the summer of 2014, and it has since become a very popular framework for .NET mobile app developers. On the surface, Xamarin.Forms is a user interface toolkit focused on abstracting the platform-specific UI APIs of iOS, Android, and Windows into a single easy-to-use set of APIs. In addition, Xamarin.Forms also provides the common components of a Model-View-ViewModel (MVVM) framework, making it extremely easy and intuitive to bind data to a user interface.

Xamarin.Forms comes with several building blocks that are paramount to a solid mobile app architecture, such as dependency injection and inversion of control, messaging, and navigation. However, many apps will quickly outgrow these "in the box" capabilities and require the use of more advanced and sophisticated replacements. This book will show you how to leverage the strengths of the Xamarin.Forms toolkit while complementing it with popular patterns and libraries to achieve a more robust and sustainable app architecture.

As with any framework or toolkit, there are specific scenarios that it makes more sense for than others. Xamarin has done a great job of providing guidance and recommendations on when the use of Xamarin.Forms is appropriate versus when it might be a better decision to use the core Xamarin platform. Once you have made the decision to use Xamarin.Forms, this book will help guide you through using patterns and best practices with your Xamarin.Forms mobile app by walking you through an end-to-end example.

What this book covers

Chapter 1, Getting Started, will start off by quickly reviewing the basics of the Xamarin.Forms toolkit. We will then walk through building a simple app with Xamarin.Forms, called TripLog. The TripLog app will serve as the foundation that we build upon throughout the rest of the book by applying new techniques and concepts in each subsequent chapter.

Chapter 2, MVVM and Data Binding, will introduce the Model-View-ViewModel (MVVM) pattern and the benefits of using it in a mobile app architecture. We will then walk through updating the TripLog app with ViewModels that provide data for the app's pages through data binding.

Chapter 3, Navigation Service, will explain how navigation works in Xamarin.Forms, some of its shortcomings, and how to overcome them. We will build a custom navigation service for the TripLog app that extends the one provided by Xamarin.Forms to provide a navigation model that occurs solely at the ViewModel level, decoupled from the pages themselves.

Chapter 4, Platform Specific Services and Dependency Injection, will discuss the power of the dependency injection and Inversion of Control (IoC) patterns, specific to multi-platform mobile development. We will discuss how to use a third-party IoC library within the TripLog app, in the place of Xamarin.Forms's default DependencyService. We will then build a couple of services that are dependent on platform-specific APIs and use them within the TripLog app through dependency injection.

Chapter 5, User Interface, will explain how to tap into platform-specific APIs using custom renderers in Xamarin.Forms. We will also discuss the use of value converters to customize the appearance of data at the time of binding.

Chapter 6, API Data Access, will explain how to set up a new API using Microsoft Azure App Services. We will then walk through how to connect the TripLog app to the API to get its data and how to set up caching for offline use.

Chapter 7, Authentication, will explain how to set up authentication on the API created in the previous chapter, and then how to add sign in and authentication to the TripLog app.

Chapter 8, Testing, will discuss the importance of testing in mobile apps. We will walk through how to take advantage of the patterns introduced throughout the book to easily unit test the ViewModels within the TripLog app. We will also use Xamarin's UITest framework to build automated UI tests for the TripLog app that can be run on Xamarin Test Cloud.

Chapter 9, App Analytics, will explain the importance of crash reporting and collecting user data in mobile apps. We will then integrate Xamarin Insights into the TripLog app using the service dependency pattern implemented in Chapter 4, Platform Specific Services and Dependency Injection.

Because the focus of this book is on applying patterns and best practices to apps built with Xamarin.Forms and not on the actual specifics of Xamarin.Forms, the chapters will only use a single platform, iOS, for simplicity. However, the architectural concepts in the book will apply to all platforms and any platform-specific code, such as platform services or custom renderers, will be included for iOS, Android, and Windows within the example code that is available for download with the purchase of this book.

What you need for this book

In order to follow along with the code throughout this book, you will need to have Xamarin installed on your machine. The Xamarin installer will take care of installing any SDK prerequisites and will also install Xamarin Studio. Although the examples throughout this book are shown in Xamarin Studio on a Mac, everything shown can also be done in Visual Studio on a Windows machine. If you are using a Windows machine, you will need a Mac running Xamarin on your network to serve as a build host to build and debug iOS apps. For details on setting up a Xamarin build host on a Mac or any other requirement to set up a Xamarin development environment, visit http://developer.xamarin.com/.

In Chapter 6, API Data Access, you will need a Microsoft Azure account in order to follow along with the examples to create a basic API.

Throughout this book, there are several tools and libraries used which are obtained from NuGet and/or Xamarin's Component Store.

Who this book is for

This book is intended for C# developers who are familiar with the Xamarin platform and Xamarin.Forms toolkit. If you have already started working with Xamarin.Forms and want to take your app to the next level, making it more maintainable, testable, and flexible, then this book is for you.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, folder names, filenames, file extensions, pathnames, URLs, user input, and Twitter handles are shown as follows: "Add a TripLogEntry property to the class and name it Entry."

A block of code is set as follows:

public MainViewModel : BaseViewModel
{
    public MainViewModel ()
    { ... }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public MainViewModel : BaseViewModel
{
    public MainViewModel ()
    {
       Entry = new TripLogEntry ();
    }
}

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/Mastering_XamarinForms_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.

Sources

The iPhone frames used to surround the screenshots in this book were developed by Andre Revin (@andre_revin). They are available for free on his website at http://andrerevin.com/iOS7-iPhone-Free-Mockup.