The final version of this ebook is now available; download the final version here.
The second preview edition of Charles Petzold’s Creating Mobile Apps with Xamarin.Forms: Cross-platform C# programming for iOS, Android, and Windows Phone is now available for download! Below you’ll find the download link and the ebook’s full Introduction. Enjoy!
This is the second Preview Edition of a book about writing applications for Xamarin.Forms, the exciting new mobile development platform for iOS, Android, and Windows Phone unveiled by Xamarin in May 2014. Xamarin.Forms lets you write shared user-interface code in C# and XAML (the Extensible Application Markup Language) that maps to native controls on these three platforms.
This book is a Preview Edition because it’s not complete. It has only 16 chapters and doesn’t cover some important topics. The final edition of the book will probably be published in the summer of 2015.
Who should read this book
This book is for C# programmers who want to write applications for the three most popular mobile platforms—iOS, Android, and Windows Phone—with a single code base. Xamarin.Forms also has applicability for those programmers who want eventually to use C# and the Xamarin.iOS and Xamarin. Android libraries to target the native application programming interfaces (APIs) of these platforms. Xamarin.Forms can be a big help in getting programmers started with these platforms or in constructing a prototype or proof-of-concept application.
This book assumes that you know C# and are familiar with the use of the .NET Framework. However, when I discuss some C# and .NET features that might be somewhat new to recent C# programmers, I adopt a somewhat slower pace.
Conventions and features in this book
This book has just a few typographical conventions:
- All programming elements referenced in the text—including classes, methods, properties, variable names, etc.—are shown in a monospaced font, such as the StackLayout class.
- Items that appear in the user interface of Visual Studio or Xamarin Studio, or the applications discussed in these chapters, appear in boldface, such as the Add New Project dialog.
- Application solutions and projects also appear in boldface, such as MonkeyTap.
The 1st and 2nd Preview Editions
This book is intended as a tutorial to learn Xamarin.Forms programming. It is not a replacement for the online API documentation, which can be found at the Xamarin.Forms Framework link on this page: http://api.xamarin.com/.
The first Preview Edition of this book was published in October 2014 to coincide with the Xamarin Evolve conference. It contained six chapters but no coverage of XAML.
This second Preview Edition was reconceived to contain shorter and more focused chapters. After the book establishes a solid foundation in code-only Xamarin.Forms programning, XAML is introduced in Chapter 7, and from that point on, XAML is used to define the user interface of most of the sample programs.
This Preview Edition unfortunately ends without covering some essential topics. The most obvious missing topics include the Grid, collection views such as ListView, page navigation, triggers, behaviors, and maps. However, it is our intention to publish chapters online just about as quickly as I can write them. New chapters will appear on the Xamarin webpage devoted to this book.
This book assumes that you’ll be using Xamarin.Forms to write applications that simultaneously target all three supported mobile platforms—iOS, Android, and Windows Phone. However, it’s very likely that many readers will be targeting only one or two platforms in their Xamarin.Forms solutions. The platforms you target—and the Xamarin Platform package you purchase—govern your hardware and software requirements. For targeting iOS devices, you’ll need a Mac installed with Apple Xcode as well as the Xamarin Platform, which includes Xamarin Studio. For targeting Windows Phone, you’ll need Visual Studio 2012 or later (not an Express Edition) on a PC, and you’ll need to have installed the Xamarin Platform.
However, you can also use Visual Studio on the PC to target iOS devices if the Mac with Xcode and the Xamarin Platform are accessible via WiFi. You can target Android devices from Visual Studio on the PC or from Xamarin Studio on either the PC or Mac.
Chapter 1 has more details on the various configurations you can use and resources for additional information and support. My setup for creating this book consisted of a Microsoft Surface Pro 2 (with external monitor, keyboard, and mouse) installed with Visual Studio 2013 and the Xamarin Platform, connected by WiFi with a MacBook Pro installed with Xcode and the Xamarin Platform.
All the screen shots in this book show an iPhone, an Android phone, and a Windows Phone in that order. The three devices shown in these screen shots reflect my setup and hardware:
- The iPhone 6 simulator on the MacBook Pro running iOS 8.2
- An LG Nexus 5 running Android 5.1.
- A Nokia Lumia 925 running Windows Phone 8.1
Some of the early screen shots in this book were from devices with somewhat earlier versions of the operating systems, for example Android 5.0 or 5.01 rather than 5.1.
The Xamarin.Forms programs in this book target the Xamarin.iOS Unified API and the Windows Phone 8.0 Silverlight API. (More about the Windows Phone API shortly.)
Downloads: Code samples
The sample programs shown in the pages of this book were compiled in late March 2015 with version 1.4.0 of Xamarin.Forms. The source code of these samples is hosted on a repository on GitHub: https://github.com/xamarin/xamarin-forms-book-preview-2/.
You can clone the directory structure to a local drive on your machine or download a big ZIP file. I’ll try to keep the code updated with the latest release of Xamarin.Forms and to fix (and comment) any errors that might have sneaked through.
You can report problems, bugs, or other kinds of feedback about the book or source code by clicking the Issues button on this GitHub page. You can search through existing issues or file a new one. To file a new issue, you’ll need to join GitHub (if you haven’t already).
Use this GibHub page only for issues involving the book. For questions or discussions about Xamarin.Forms itself, use the Xamarin.Forms forum: http://forums.xamarin.com/categories/xamarin-forms.
Updating the code samples
The libraries that comprise Xamarin.Forms are distributed via the NuGet package manager. The Xamarin.Forms package consists of five dynamic-link libraries:
The Xamarin.Forms package also requires:
- Xamarin Support Library v4 (Xamarin.Android.Support.v4.dll)
- Windows Phone Toolkit (Microsoft.Phone.Controls.Toolkit.dll)
These should be automatically included.
When you create a new Xamarin.Forms solution using Visual Studio or Xamarin Studio, a version of the Xamarin.Forms package becomes part of that solution. However, that might not be the latest Xamarin.Forms package available from NuGet. Here’s how to update to that package:
In Visual Studio, right-click the solution name in the Solution Explorer and select Manage NuGet Packages for Solution. Select Installed packages at the left of the dialog to see what’s currently installed, and select Updates and nuget.org at the left to choose to update the package. If an update is available, clicking the Update All button is the easiest way to get it into the solution.
In Xamarin Studio, in the individual projects in the Solution list, under Packages, select the Xamarin.Forms package and select Update from the tool drop-down.
The source code for this book that is stored on GitHub does not include the actual NuGet packages. Xamarin Studio will automatically download them when you load the solution, but by default Visual Studio will not. After you first load the solution into Visual Studio, right-click the solution name in the Solution Explorer and select Manage NuGet Packages for Solution. You should be given the option to restore the packages with a Restore button at the top of the dialog. You can then update the package by selecting Updates and nuget.org at the left and (if an update exists) pressing the Update All button.
Big changes coming for Windows Phone
As this second Preview Edition is being finalized in late March 2015, a big change for Xamarin.Forms is just becoming available in a preview release: this is support for the Windows Runtime API supported by Windows Phone 8.1 and by Windows 8.0 Windows Store applications.
The contents of this book do not reflect that change! All the programs in this book target the Silverlight API of Windows Phone 8.0. Most of these programs should migrate easily to the Windows Runtime API. However, any program that relies upon Windows Phone 8.0 platform services—in particular, the programs in Chapter 9, “Platform-specific API calls”—will need some work.
By the time you see this book, some of the work converting the sample programs to the Windows Runtime platform should already have been completed. It is anticipated that the GitHub repository with the sample code for this book will have a branch named windows-runtime with the converted samples.
It’s always seemed peculiar to me that authors of programming books are sometimes better known to programmers than the people who actually created the product that is the subject of the book! The real brains behind Xamarin.Forms are Jason Smith, Eric Maupin, Stephane Delcroix, Seth Rosetter, Rui Marinho, and Chris King. Congratulations, guys! We’ve been enjoying the fruits of your labor!
Over the months that the first and second Preview Editions were in progress, I have benefited from valuable feedback, corrections, and edits from several people. This book wouldn’t exist without the collaboration of Bryan Costanich at Xamarin and Devon Musgrave at Microsoft Press. Both Bryan and Craig Dunn at Xamarin read some of my drafts of early chapters and managed to persuade me to take a somewhat different approach to the material. Later on, Craig kept me on track and reviewed the chapters in this second Preview Edition, and John Meade did the copyediting. For the first Preview Edition, Stephane Delcroix at Xamarin and Andy Wigley with Microsoft offered essential technical reads and persistently prodded me to make the book better. Microsoft’s copyeditor for this second Preview Edition was John Pierce.
Almost nothing I do these days would be possible without the daily companionship and support of my wife, Deirdre Sinnott.
March 23, 2015