Xamarin in Action
Creating native cross-platform mobile apps
Jim Bennett
  • MEAP began September 2016
  • Publication in March 2018 (estimated)
  • ISBN 9781617294389
  • 550 pages (estimated)
  • printed in black & white

Xamarin in Action teaches you how to build Xamarin apps on iOS and Android from scratch while maximizing code re-use. This layer-by-layer guide starts by showing you the MVVM design pattern and explaining how it increases code sharing. Next, you'll start building an app from the ground up. You'll learn all the different layers inside a well written Xamarin app and discover how most of your code can be shared between iOS and Android. You'll look at how this code sharing can be best designed to allow unit testing of your business layer as well as your UI logic. Then you'll dig in to code that can't be shared, like user interface code that's device specific. Finally, you'll learn how to take your app from final code to a tested and deployed version on the relevant store. By the end, you'll be able to build a high quality production-ready Xamarin app on iOS and Android from scratch with a high level of code reuse.

"Your search for a reliable and quality Xamarin guide ends here."

~ Krishna Anipindi

"A thorough reference by example that is up-to date and illustrates the latest best practices."

~ Mario Solomou

"The book truly covers all aspects of the Xamarin development experience."

~ Gareth van der Berg

Table of Contents detailed table of contents

Part 1: Getting started with Xamarin

1 Introducing native cross-platform applications with Xamarin

1.1 Introducing Xamarin mobile apps

1.1.1 Vendor-specific native apps

1.1.2 Cordova

1.1.3 Xamarin native apps

1.1.4 Xamarin.Forms

1.1.5 Xamarin developer tools

1.1.6 Mobile-optimized development lifecycle

1.2 Creating production-quality mobile apps

1.2.1 Design

1.2.2 Develop

1.2.3 Test

1.2.4 Build

1.2.5 Distribute

1.2.6 Monitor

1.3 Rinse and repeat…

1.4 Summary

2 Hello MVVM—​creating a simple cross-platform app using MVVM

2.1 What are UI design patterns?

2.2 MVVM—​the design pattern for Xamarin apps

2.3 What is cross-platform code?

2.3.1 .NET Standard class libraries

2.4 Getting started—​creating your first solution

2.4.1 Requirements—​what hardware or software do you need for each mobile platform?

2.4.2 Creating the solution

2.4.3 What have we just created?

2.4.4 Building and running the apps

2.5 Is this really a cross-platform app?

2.6 Summary

3 MVVM—​the model-view-view model design pattern

3.1 The model layer

3.2 The view-model layer

3.2.1 State and behavior

3.2.2 Value conversion

3.2.3 Testability

3.3 The view layer

3.4 Binding

3.4.1 Source and target

3.4.2 Binding mode

3.4.3 Binding is not cross-platform

3.4.4 Value converters

3.5 The application layer

3.6 Navigation

3.6.1 View-first

3.6.2 View-model–first

3.6.3 Which one to use?

3.7 Revisiting the square-root calculator app

3.8 Summary

4 Hello Again MVVM—​understanding and enhancing our simple MVVM app

4.1 A deeper dive into our Hello Cross-Platform World app

4.1.1 The model

4.1.2 The view model

4.1.3 The application layer

4.1.4 The view

4.2 Expanding on our Hello World app

4.2.1 Using .NET Standard plugins to access device-specific code

4.2.2 Installing the Xamarin text-to-speech plugin

4.2.3 Adding the cross-platform code

4.2.4 Inversion of control

4.2.5 Wiring up the Android UI

4.2.6 Wiring up the iOS UI

4.3 Summary

5 What are we (a)waiting for? An introduction to multithreading for Xamarin apps

5.1 Why do we need multithreaded code?

5.2 What are threads?

5.2.1 Buying coffee

5.2.2 So what is a thread?

5.2.3 Quick round-up

5.3 UI thread and background threads

5.3.1 The UI thread

5.3.2 Background threads

5.4 Using tasks to run code in the background

5.4.1 Task and Task<T>

5.4.2 Chaining tasks

5.5 Task results

5.5.1 Polling to see if the task has finished

5.5.2 Waiting on the task

5.5.3 Getting the result from a continuation

5.5.4 Task exceptions

5.6 Updating the UI

5.6.1 The UI task scheduler

5.6.2 Using the power of MVVM

5.7 Async and await

5.7.1 The async and await keywords

5.7.2 Writing your own async methods

5.7.3 Async commands

5.8 Make your app feel responsive

5.9 It’s time to start building things

5.10 Summary

Part 2: Building apps

6 Designing MVVM cross-platform apps

6.1 Introduction to designing a cross-platform app

6.2 Designing the UI and user flows

6.2.1 SquareRt—​a simple app for calculating square roots

6.2.2 Countr—​an app for counting multiple things

6.2.3 Defining user flows and UIs

6.3 Architecting the app

6.3.1 Which layer?

6.3.2 Which thread?

6.3.3 Mapping code to layers and threads

6.4 Creating the solutions

6.5 Application properties

6.5.1 Android manifest

6.5.2 iOS info.plist

6.6 SDK versions

6.6.1 Android SDK versions and the SDK manager

6.6.2 iOS SDK versions

6.7 Linking

6.7.1 Linking the apps

6.7.2 Linker options

6.7.3 Stopping the linker from doing too much

6.8 Summary

7 Building cross-platform models

7.1 Building simple model layers

7.2 Unit testing

7.2.1 Creating a unit-test project

7.2.2 Creating your first test

7.2.3 What do these tests tell you?

7.3 Building more complex model layers

7.3.1 Services, data models, and repositories

7.3.2 Accessing databases

7.3.3 Adding a service layer

7.3.4 Accessing web services

7.4 A recap

7.5 Summary

8 Building cross-platform view models

8.1 The view-model layer

8.1.1 The view-model layer inside SquareRt

8.1.2 The view-model layer inside Countr

8.2 Adding state and behavior to SquareRt

8.2.1 State inside SquareRt

8.2.2 Exposing behavior via property changes

8.3 Adding state and behavior to Countr

8.3.1 Single-value properties

8.3.2 Collections

8.3.3 Exposing behavior using commands

8.3.4 Messaging

8.3.5 Navigation

8.4 A quick roundup

8.5 Summary

9 Building simple Android views

9.1 Building Android UIs

9.1.1 Material design

9.1.2 Layout files

9.1.3 Resources

9.1.4 Resource locations

9.1.5 Editing layout files

9.1.6 Layout inflation

9.2 Creating the layout file for the SquareRt UI

9.2.1 Adding a toolbar

9.2.2 Adding an image

9.2.3 Adding an EditText control

9.2.4 Adding a result TextView control

9.3 Building the SquareRt view

9.3.1 What is an activity

9.3.2 The activity lifecycle

9.3.3 Creating an activity for the view

9.3.4 Running the app

9.4 Summary

10 Building more advanced Android views

10.1 Building the UI for Countr

10.1.1 Creating the UI for the master view

10.1.2 Recycler views

10.1.3 Creating the UI for the recycler view items

10.1.4 Floating action buttons

10.1.5 Creating the UI for the detail view

10.1.6 Menu items

10.2 Building the Countr activities

10.2.1 Setting up master recycler views

10.2.2 The detail view

10.2.3 Running the app

10.3 App icons and launch screens

10.3.1 App icons

10.3.2 Launch screens

10.4 Summary

11 Building simple iOS views

11.1 Building iOS UIs

11.1.1 iOS human interface guidelines

11.1.2 Storyboards

11.1.3 Controls

11.1.4 Different screen resolutions

11.1.5 Auto layout with constraints

11.1.6 Image resources and asset catalogs

11.1.7 A quick recap

11.2 Creating the SquareRt storyboard

11.2.1 Adding our first view controller

11.2.2 Adding an image

11.2.3 Adding a text field

11.2.4 Adding the result label

11.2.5 Seeing the layout on different devices

11.2.6 Size classes

11.2.7 A quick recap

11.3 Building the SquareRt view

11.3.1 What is a view controller

11.3.2 View lifecycle

11.3.3 Creating the view controller

11.3.4 Wiring up controls to the view controller

11.3.5 Binding the view controller

11.3.6 Another quick recap

11.3.7 Running the app

11.4 Summary

12 Building more advanced iOS views

12.1 Building the UI and view controllers for Countr

12.1.1 Creating the UI for the master view

12.1.2 Navigation bars and buttons

12.1.3 Creating the UI for the detail view

12.1.4 A quick recap

12.1.5 Running the app

12.2 App icons and launch screens

12.2.1 App icons

12.2.2 Launch screens

12.3 Making the apps production-ready

12.4 Summary

Part 3: From working code to the store

13 Running mobile apps on physical devices

13.1 Running Android apps on a real device

13.2 Signing Android apps for publishing

13.2.1 Setting the package name

13.2.2 Keystores

13.2.3 Creating keystores and signing builds

13.3 Running iOS apps on a real device

13.3.1 What is a provisioning profile?

13.3.2 Bundle identifiers

13.3.3 Creating a dummy app in Xcode

13.3.4 Running your app on a physical device

13.4 Creating iOS provisioning profiles

13.4.1 Certificates

13.4.2 App IDs

13.4.3 Devices

13.4.4 Provisioning profiles

13.4.5 Running your app using the new provisioning profile

13.4.6 Troubleshooting

13.5 Summary

14 Testing mobile apps using Xamarin UITest

14.1 Introduction to UI testing

14.1.1 Writing UI tests using Xamarin UITest

14.1.2 Setting up your app for UI testing

14.1.3 Running the auto-generated tests

14.2 Writing tests

14.2.1 The visual tree

14.2.2 The REPL

14.2.3 Identifying controls

14.2.4 Tapping the Add button

14.2.5 Entering text

14.2.6 Finding controls based on their text

14.2.7 Assertions

14.2.8 Proving your test by breaking things

14.3 Testing incrementing a counter

14.4 The app interface and app queries

14.4.1 The IApp interface

14.4.2 Queries

14.5 Summary

15 Using App Center to build, test and monitor apps

15.1 Introducing Visual Studio App Center

15.1.1 Apps

15.1.2 Users and organizations

15.1.3 API

15.1.4 CLI

15.1.5 Getting help

15.2 Setting up builds

15.2.1 Creating your first App Center app

15.2.2 Configuring the Android build

15.2.3 Configuring the iOS build

15.3 Testing your apps using Test Cloud

15.3.1 What is Test Cloud

15.3.2 Preparing your apps to be tested

15.3.3 Creating a test run configuration

15.3.4 Running your tests from the command line

15.3.5 Viewing the test results on App Center

15.4 Analytics and crash reporting

15.4.1 Adding the App Center SDKs

15.4.2 Understanding your audience

15.4.3 Adding event tracking

15.4.4 Crash reporting

15.5 Summary

16 Deploying apps to beta testers and the stores

16.1 Distributing Android apps to beta testers

16.1.1 Enabling app distribution

16.1.2 Auto updates

16.2 Publishing Android apps on the Google Play store

16.2.1 Setting up your account

16.2.2 Creating your app

16.2.3 Alternative stores

16.3 Distributing iOS apps to beta testers

16.3.1 Enabling app distribution

16.3.2 Auto updates

16.4 Publishing iOS apps on the Apple App store

16.4.1 Provisioning your app for publishing

16.4.2 Setting up your app

16.5 Summary

16.6 Where to next?

Appendixes:

Appendix A: UI flows and thread for SquareRt and Countr

A.1 SquareRt

A.2 Countr

A.2.1 Loading counters

A.2.2 Adding a counter

A.2.3 Deleting a counter

A.2.4 Incrementing a counter

Appendix B: Using MVVM Light instead of MvvmCross

B.1 MVVM Light

B.2 Installing MVVM Light

B.3 The model layer

B.4 The view model layer

B.5 The view layer

B.5.1 The Android view and application layer

B.5.2 The iOS view and application layer

B.6 Summary

About the Technology

Xamarin is a toolset that allows you to write native mobile apps in C# and run them on both iOS and Android devices. What makes Xamarin stand out from other cross-platform tools is that it gives you the ability to share large portions of code across these two platforms while still letting you write native apps that can take full advantage of the device and OS features specific to each platform. And since Xamarin was recently acquired by Microsoft, you can be sure the ecosystem will continue to grow - Xamarin has become a hugely important part of Microsoft's "any developer, any platform" mantra.

What's inside

  • Understand MVVM and how it maximizes code reuse and testability
  • Build a mobile app from rough design through final deployable product
  • Creating cross platform model and UI logic layers
  • Build device specific UIs for iOS and Android
  • Test apps through unit and automated UI testing
  • Prepare apps for publication with user tracking and crash analytics

About the reader

This book is for C# developers with a few months through many years of experience who want to build native mobile apps for iOS and Android using the language and toolset they already know.

About the author

Jim Bennett is a Senior Cloud Developer Advocate at Microsoft, specializing in cloud-connected Xamarin apps. He has decades of experience building desktop and mobile apps, mainly using C# and other Microsoft technologies. For the past four years he has been heavily involved in developing cross-platform mobile apps using Xamarin, both at work and as personal projects. He's a regular speaker on mobile development at meetups and conferences, contributes to open source, and blogs about and evangelizes Xamarin whenever he can. He's a former Xamarin and Microsoft MVP, he's passionate about sharing knowledge and helping others to learn, and when he's not playing with his young daughter, he's happy to spend hours discussing mobile development over Thai food and good beer or whisky.