Book Image

Mastering Xamarin.Forms - Third Edition

By : Ed Snider
Book Image

Mastering Xamarin.Forms - Third Edition

By: Ed Snider

Overview of this book

Discover how to extend and build upon the components of the most recent version of Xamarin.Forms to develop an effective, robust mobile app architecture. This new edition features Xamarin.Forms 4 updates, including CollectionView and RefreshView, new coverage of client-side validation, and updates on how to implement user authentication. Mastering Xamarin.Forms, Third Edition is one of the few Xamarin books structured around the development of a simple app from start to finish, beginning with a basic Xamarin.Forms app and going step by step through several advanced topics to create a solution architecture rich with the benefits of good design patterns and best practices. This book introduces a core separation between the app's user interface and the app's business logic by applying the MVVM pattern and data binding, and then focuses on building a layer of plugin-like services that handle platform-specific utilities such as navigation and geo-location, as well as how to loosely use these services in the app with inversion of control and dependency injection. You’ll connect the app to a live web-based API and set up offline synchronization before testing the app logic through unit testing. Finally, you will learn how to add monitoring to your Xamarin.Forms projects to track crashes and analytics and gain a proactive edge on quality.
Table of Contents (12 chapters)

Creating a base HTTP service

In order for an app to communicate with an API via HTTP, it needs an HTTP library. Since we are using .NET and C# to build a Xamarin.Forms app, we can leverage a library called System.Net.Http.HttpClient. The .NET HttpClient provides a mechanism to send and receive data via standard HTTP methods, such as GET and POST.

Continuing to keep separation and abstraction key to our app architecture, we want to keep the specific logic related to the HttpClient separate from the rest of the app. In order to do this, we will write a base service class in our core library that will be responsible for handling HTTP communications in a generic way. This provides a building block for any domain-specific data services we might need to write; for example, a service that is responsible for working with log entries in the API. Any class that will inherit from this class will be able to send HTTP request messages using standard HTTP methods (such as GET, POST, PATCH, and...