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, data binding, 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 where Xamarin.Forms might make more sense 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.

Who this book is for

This book is intended for .NET 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.

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 context for the app's pages through data binding. We will then build client-side validation that leverages the MVVM pattern put in place.

Chapter 3, Navigation, will explain how navigation works in Xamarin.Forms and some approaches to navigation related to MVVM. 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 inversion of control (IoC) and the dependency injection pattern, specific to multi-platform mobile app development. We will discuss the Xamarin.Forms Dependency Service and some of its shortcomings. We will add a third-party dependency injection library to the TripLog app to be used instead of Xamarin.Forms's default Dependency Service. We will then build some 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 user interface 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 RESTful API using a Microsoft Azure Function App. 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 Chapter 6, API Data Access, and then how to add sign-in, sign-out, 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.

Chapter 9, App Monitoring, will explain the importance of crash reporting and collecting analytical data in mobile apps. We will then integrate the Visual Studio App Center SDK into the TripLog app using the service dependency pattern implemented in Chapter 4, Platform-Specific Services and Dependency Injection.

To get the most out of this book

To get the most out of this book, you should have a working knowledge of the Xamarin platform and Xamarin.Forms toolkit as well as experience with .NET.

In order to follow along with the code throughout this book, you will need to have Visual Studio and Xamarin installed on your Windows or Mac computer. Although the examples throughout this book are shown in Visual Studio for Mac, everything shown can also be done in Visual Studio for Windows. If you are using a Windows computer, you will need a Mac running Xamarin on your network to serve as a build host to build and deploy iOS apps. For details on setting up a Mac build host or any other requirements for setting up a Xamarin development environment, visit docs.microsoft.com/en-us/xamarin.

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 using an Azure Function App.

Throughout this book, there are several open source tools and libraries used, which are obtained from NuGet via the Visual Studio package manager.

Download the example code files

You can download the example code files for this book 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 emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at http://www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  1. Enter the name of the book in the Search box and follow the on-screen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mastering-Xamarin.Forms-Third-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781839213380_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, class names, property names, field names, folder names, filenames, file extensions, pathnames, URLs, and user input. For example; "Update the constructor in the BaseViewModel class."

A block of code is set as follows:

public class BaseViewModel
{
    protected BaseViewModel()
    {
    }
}

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 class BaseViewModel : INotifyPropertyChanged
{
    protected BaseViewModel()
    {
    }
}

Bold: Indicates a new term, an important word, or words that you see on the screen, for example, in menus or dialog boxes, also appear in the text like this. For example: "In Visual Studio, click on File | New Solution."

Warnings or important notes appear like this.

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected], and mention the book's title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book we would be grateful if you would report this to us. Please visit, http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.141.6