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)

Creating a base HTTP service


In order for an app to communicate with an API via HTTP, it needs an HTTP library. Because we are using .NET and C# to build a Xamarin.Forms app, we can leverage a library within the .NET Framework 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 will want to keep the specific logic related to the HttpClient separated 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...