Book Image

Xamarin Cross-Platform Development Cookbook

By : George Taskos
Book Image

Xamarin Cross-Platform Development Cookbook

By: George Taskos

Overview of this book

<p>You can create native mobile applications using the Xamarin Forms platform for the three major platforms iOS, Android, and Windows Phone. The advantage of this is sharing as much code as you can, such as the UI, business logic, data models, SQLite data access, HTTP data access, and file storage across the three major platforms.</p> <p>This book provide recipes on how to create an architecture that will be maintainable, extendable, use Xamarin Forms plugins to boost productivity, customize your views per platforms, and use platform-specific implementations at runtime.</p> <p>We start with a simple creation of a Xamarin Forms solution with the three major platforms. We will then jump to XAML recipes and you will learn how to create a tabbed application page, and customize the style and behavior of views for each platform. Moving on, you will acquire more advanced knowledge and techniques while implementing views and pages for each platform and also calling native UI screens such as the native camera page.</p> <p>Further on, we demonstrate the power of architecting a cross-platform solution and how to share code between platforms, create abstractions, and inject platform-specific implementations. Next, you will utilize and access hardware features that vary from platform to platform with cross-platform techniques. Well then show you the power of databinding offered by Xamarin Forms and how you can create bindable models and use them in XAML. You will learn how to handle user interactions with the device and take actions in particular events.</p> <p>With all the work done and your application ready, you will master the steps of getting the app ready and publishing it in the app store.</p>
Table of Contents (18 chapters)
Xamarin Cross-Platform Development Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

There is no better time for advancing your toolbox with cross-platform mobile development using Xamarin.Forms. Xamarin has over 1,300,000 registered developers and 15,000 clients, it is now the standard in enterprise mobility, and the demand for cross-platform Xamarin mobile developers is very high. The idea behind Xamarin.Forms is that you're no longer only able to share your business logic but the UI code across iOS, Android and Windows Phone. With Xamarin, you can open your favorite IDE, Visual Studio, or use Xamarin Studio, to create cross-platform applications using C# while still generating and deploying a 100% native platform application.

If you know C#, you know how to create native cross-platform, and this book will just make it easier for you.

You will learn everything available to combine your application and build RAD mobile applications. Even if there is a requirement to work on the native application layers, you will find step-by-step recipes that provide you with the knowledge and understanding of how to accomplish your goal.

Work with the UI in XAML or in code, and learn how every control and page is mapped to each equivalent native UI component. Create custom views, call platform APIs, and explore all the cross-platform types of pages, layouts and controls. Create your own cross-platform plugin, and deploy it to NuGet for other developers or commercial purposes.

At the core of the book, the focus is on cross-platform architecture: how to efficiently share code between all platforms, use the built-in dependency service locator, and configure your solution to use a third-party dependency injection using aspect-oriented programming.

Using a cross-platform UI framework doesn't mean that your UI should be coupled to the rest of your code; in this book, the MVVM architecture is demonstrated by injecting the ViewModel in the XAML and using data binding to sync your data between UI controls and models, keeping clean the separation of concerns.

What is programming? Art? Are we the crafts people? No. We process data, that's what we do from the backend to the frontend and vice versa, fetching, transforming, accepting input changes and persisting back to a storage. Data access is the most important layer in an application and there are recipes to cook the repository pattern for local and remote data with many tips, tricks, and best practices for performance and efficiency.

These sequences of data are often presented in a list control. Continuing from this, the book covers the practices needed to create and customize a Xamarin.Forms ListView, adding grouping, jump list, and custom cells.

No modern application is built today without unit testing; in addition, you will find recipes that will help you understand and leverage acceptance UI testing locally and uploading them in Xamarin Test Cloud testing in thousands of physical devices.

Towards the end, we will focus on monitoring an application using Xamarin Insights and preparing and packaging each native platform to upload to the corresponding marketplaces.

What this book covers

Chapter 1, One Ring to Rule Them All, shows you how to work with Xamarin Forms, how to get started with a simple project, understand the structure, and make use of available controls. Starting out the first chapter with creating a cross-platform solution using Xamarin Studio and Visual Studio, create your first first page, use Xamarin's out-of-the-box common platform APIs, and learn how to access the native platform page renderers.

Chapter 2, Declare Once, Visualize Everywhere, shows you how to use the Xamarin Forms XAML declarative language to create cross-platform UI. This chapter introduces you to how to create a tabbed application and add UI behaviors and triggers; the main focus is user interface development and providing knowledge in view renderers.

Chapter 3, Native Platform-Specific Views and Behavior, teaches you how to create different page layouts and custom controls per platform. Cross-platform development has all the benefits of sharing code across all three platforms, though there are many scenarios in which you need to access the native platform layer. This chapter will give you all the recipes to create custom platform views, add platform-specific gestures, and access the camera platform APIs.

Chapter 4, Different Cars, Same Engine, shows you how to apply cross-platform architecture, patterns, and practices. Leverage the power of Xamarin.Forms with six sections with all the ins and outs of best practices for sharing code between platform. Use the built-in dependency locator to resolve implementation classes and publish messages to components. Design an MVVM solution, add a third-party DI container for Aspect Oriented Programming and localize your applications for any languages.

Chapter 5, Dude, Where's my Data?, shows you how to create a cross-platform data access component fetching data from a local SQLite database and a REST web service. Separation of concerns in a cross-platform solution is critical, learn how to create efficient web and local database repositories. Leverage the native platform performant HTTP SDKs adding and configuring a NuGet package.

Chapter 6, One for All and All for One, shows you how to use Xamarin plugins to access native platform capabilities like the camera, GPS, and showing local notifications. The Xamarin community is very generous, there are plugins for Xamarin almost for anything you might want or learn how to do it adding your own implementation, and then share it with giving back or maybe as a commercial library. Learn how to create your own cross-platform plugins and how to use plugins for photos, GPS and local notifications.

Chapter 7, Bind to the Data, shows you how to leverage the built-in Xamarin Forms data-binding mechanism. Databinding is not a concept that every native platform is providing out of the box, and if you can't actually use data-binding the MVVM architecture doesn't sound such a great idea. Xamarin.Forms provides an out of the box mechanism to bind your data in code or declarative XAML.

Chapter 8, A List to View, teaches you how to bind collections to ListView, customize its appearance with custom cells and apply grouping.

Chapter 9, Gestures and Animations, shows you how to add cross-platform animations shared between iOS, Android, and Windows Phone and handle user gestures in XAML and in native platform renderers.

Chapter 10, Test Your Applications, You Must, shows you how to create unit tests for your portable shared code, and platform-specific unit tests. Create UI acceptance tests and run them locally or in Xamarin Test Cloud. Learn how to use Calabash and REPL.

Chapter 11, Three, Two, One – Launch and Monitor, shows you how to add real-time monitoring to get detailed error reports. Prepare and package your applications for submission in iOS, Android, and Windows Phone stores.

What you need for this book

  • On Mac:

    • Xamarin Studio 5.10.1 (build 6)

    • The latest iOS SDK (Currently in version 9.2)

    • Xcode 7.1

    • OS X 10.10.5+ (Yosemite) or 10.11 (El Capitan)

  • On Windows:

    • Any non-Express edition of Visual Studio 2012

    • Visual Studio 2013

    • Visual Studio 2015 (Community, Professional, and Enterprise)

  • Visual Studio Extensions for iOS and Android

All examples will work with the Free Trial evaluation or Xamarin Starter licenses. Xamarin Starter is installed by default with Visual Studio 2015 and works with VS 2012, 2013, and 2015 (including Community editions).

Who this book is for

This book is for mobile developers. You must have some basic experience of C# programming, but no previous experience with Xamarin is required. If you are just starting with C# and want to use Xamarin to develop cross-platform apps effectively and efficiently, then this book is the right choice for you.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it…, How it works…, There's more…, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

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, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "With the Auth.GetUI() method, we get a platform-specific object that we can use to show our authentication view."

A block of code is set as follows:

Position position = new Position(40.730599, -73.986581);
Pin pin = new Pin {
  Type = PinType.Place,
  Position = position,
  Label = "New York",
  Address = "New York"
};

map.Pins.Add(pin);

Any command-line input or output is written as follows:

[0:] Exception: System.NotSupportedException: Need to fix this!
  at XamFormsMessagingCenter.MainPage.OnAppearing ()………..

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: " Start Visual Studio and create a new Blank App (Xamarin.Forms Portable) project."

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 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 e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

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

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.