33,59 €
Design, develop, and publish your own mobile apps for iOS and Android using C# and Xamarin Studio
If you want to develop your own applications and want to explore the features of Xamarin Studio, then this is the book for you. It is expected that you have a basic understanding of technologies in mobile development, but prior knowledge of Xamarin is not required.
The mobile app market is increasing exponentially every year. Xamarin Studio with its modern and powerful IDEs makes creating applications a lot easier by simplifying the development process. Xamarin will allow you and your team to create native applications by taking advantage of one of the most evolved programming language in the world: C#.
This book will provide you with the basic skills you need to start developing mobile apps using C# and Xamarin. By working through the examples in each chapter, you will gain hands-on experience of creating a complete app that is fully functional by all means. Finally, you will learn to publish the app you created on the app market. Each project in this book will take you one step closer to becoming a professional app developer.
The step-by-guide will walk you through the process of creating an application of with the help of small projects that will teach you everything you need to know to build a complete application of your own.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 206
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: August 2016
Production reference: 1250816
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-290-4
www.packtpub.com
Matteo Bortolu
Engin Polat
Copy Editor
Safis Editing
Reviewers
Betim Drenica
Matheus Guimaraes
Paul Leman
Tom Opgenorth
Chris van Wyk
Project Coordinator
Shweta H Birwatkar
Commissioning Editor
Veena Pagare
Proofreader
Safis Editing
Acquisition Editor
Meeta Rajani
Indexer
Aishwarya Gangawane
Content Development Editor
Deepti Thore
Graphics
Disha Haria
Technical Editor
Vivek Arora
Production Coordinator
Nilesh Mohite
Matteo Bortolu currently works in Singapore for Sixscape Communication as Lead Mobile Developer.
He grew up with a strong passion for IT and right after his master of science degree in 2006, the software industry transformed his biggest talent and passion into an enthusiastic software developer.
After more than 20,000 hours of writing backend and frontend solutions based on Microsoft technologies, he met Xamarin in 2012 and felt in love with it.
He has played key roles in mobile projects for worldwide customers, deploying to the stores a wide category of apps such as games, health industry apps, messaging apps, energy industry apps, virtual reality apps, and others.
When he is not in front of a laptop he loves reading, playing the saxophone, and exploring this planet. He loves to share his experiences on his blog (http://bortolu.com).
Back in 2014 he founded the Xamarin Developers Facebook group (https://www.facebook.com/groups/xamarin.developers), which currently has more than 14,000 members.
Engin Polat has been involved in many large-and medium-scale projects on .NET technologies as a developer, architect, and consultant and has won many awards since 1999.
Since 2008, he has given training to many large enterprises in Turkey about Windows development, web development, distributed application development, software architecture, mobile development, cloud development, and more.
Apart from this, he organizes seminars and events in many universities in Turkey about .NET technologies, Windows platform development, cloud development, web development, and game development.
He shares his experiences on his personal blog (http://www.enginpolat.com). He has MCP, MCAD, MCSD, MCDBA, and MCT certifications. Since 2012 he has been recognized as a Windows Platform Development MVP (Most Valuable Professional) by Microsoft. Between 2013 and 2015, he was recognized as a Nokia Developer Champion; very few people in the world are given this award. Since 2015 he has been recognized as a Regional Director by Microsoft.
He has also reviewed Mastering Cross-Platform Development with Xamarin and Xamarin Blueprints.
I'd like to thank my dear wife, Yeliz, and my beautiful daughter, Melis Ada, for all the support they gave me while I was working on this book project.
Matheus Guimaraes is the founder and CEO of Guimak Ltd. He’s been in the industry since 2002 and served as CTO, principal architect, and technical consultant for various companies over the years. He’s been involved in many projects, including The Daily Mail, Xbox, Moonpig, Tesco, and PRS for Music. His latest passion is developing games with Unity and mobile apps with Xamarin. He is a certified Xamarin developer, and his company has been a Xamarin consulting partner since 2015.
Chris van Wyk is a Xamarin University trainer with 18 years of experience in the IT industry. In his various roles as developer, team lead, architect, and software development manager, Chris has been involved in both backend and frontend software development and delivery. With the initial releases of MonoTouch and MonoDroid in 2010, now Xamarin, the development story of mobile was too enticing not to explore. Chris believes Xamarin is the perfect development platform for developers to create applications that delight users across mobile platforms.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com , you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Microsoft released .NET Framework in February 2002 for Windows platform. The Mono Project was released in June 2004, and it brought .NET to Linux and Mac OS. In 2 years, the Mono Project creators saw a potential in C# and .NET, but they progressed slowly and in 2011, the Mono Project version 1.1 was released.
The Mono Project evolved in time and transformed into a huge cross-platform framework, changing its name to Xamarin.
In February 2016, Microsoft announced that it had acquired Xamarin, and later it was made free and open source.
At the time of writing, Microsoft is the biggest company investing in cross-platform development and helping developers to build applications easily.
Xamarin has several components that develop, build, and package projects in order to publish them on stores. A few such examples are Xamarin.Android, Xamarin.iOS, and Xamarin.Forms. Xamarin.Android and Xamarin.iOS solutions are targeting individual platforms such as Android and iOS. On the other hand, Xamarin.Forms targets all platforms in one solution.
In this book, you'll learn how to use Xamarin.Forms to develop cross-platform applications with different page types, layouts, views, and design patterns by using them.
Chapter 1, Getting Started with Xamarin, will help us discover the basics of cross-platform development and where its latest version Xamarin 4 fits. We'll also learn how to use the latest version, Xamarin Studio 6, as the IDE.
Chapter 2, Sharing Code between Platforms, will differentiate between Portable Class Libraries and Shared Projects. We will also explore the fundamentals of the MVVM pattern by using it.
Chapter 3, Exploring the UI Controls, will explain all the page types, layout types, view elements, and rendering models provided by the Xamarin framework out of the box.
Chapter 4, Data – the Monkeys Catalog, will show how the readers to create base types of entities, data access layers, business layers in order to use them along with any project that we'll develop. We'll also create core implementations of them.
Chapter 5, Cloud and Async Communication, will help us explore different formats, data, and channel types when communicating with a remote server. We'll explore the differences between a RESTful service and a WSDL service and develop a sample application.
Chapter 6, Custom Renderers, will describe customer renderers by creating one. Also, we'll learn to use AppLinks by example.
Chapter 7, Monkey Puzzle Game – Processing Images, will help us develop an example project from scratch. We'll develop custom renderers to complete the project.
Chapter 8, The People Around Me Application, explains how to develop an example project from scratch. We'll start preparing our development machine and end with a ready-to-publish application. We'll develop and communicate with a web backend in this example project.
Chapter 9, Testing – Spot the Bugs, will explain the importance of debugging, testing, and profiling. We'll learn about the different log panels of Xamarin Studio 6. We'll also learn the fundamentals of Xamarin Profiler and the Xamarin.UITest Framework.
Chapter 10, Publishing to the Market, helps us finalize this book by publishing a project to all three stores. Starting from building the project, we'll investigate the steps of creating developer accounts, readying the publish package, and uploading them to the stores.
You'll need a computer and reliable Internet connection. Here is a full-featured list of the required applications:
Apple requires iOS applications to be compiled on a Mac computer, Xamarin requires as well. All required applications can be downloaded from http://xamarin.com/download and https://www.visualstudio.com pages.
This book is great if you’re already familiar with C# and want to break down the walls of developing applications to a single platform. It’s assumed that you have a good knowledge of the object-oriented programming paradigm.
If you want to be familiar with developing applications to all three platforms (Windows, Android, and iOS), this book is for you.
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: "We can start creating a folder called Base and a folder called Core inside the main Xamarin Form project."
A block of code is set as follows:
using SQLite.Net.Attributes; namespace XamarinByExample.MonkeysCatalogue { public class BaseEntity<TKey> { [PrimaryKey] public TKey Key { get; set; } } }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: "To use a WSDL, we need to right-click on the project and select Add a Web Reference:"
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
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 [email protected], 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.
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.
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:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Xamarin-4-By-Example. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
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 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 [email protected] with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
Ladies and gentlemen, this is Engin and Matteo and we are your chief flight attendants. On behalf of Xamarin Developers Crew, welcome aboard! This is a non-stop service from C# to multiplatform mobile development. Our flight time will be as long as you prefer. We will be exploring exciting examples, trying to cover all the basic and some advanced topics of mobile development. Now make sure your seat and desktop are in the most comfortable position. At this time, we request that all electronic devices be switched in to developer mode. You will find this and all the other useful information in the book located in front of you. We strongly suggest that you read it before take-off. If you have any questions, don't hesitate to e-mail us. We wish you all an enjoyable flight.
Before we learn what cross-platform is, we will explore the meaning of platform specific native apps.
A native app is an app that uses the native Software Development Kit (SDK), and compiles and runs on one specific platform.
It is usually developed using the default programming language of the SDK.
We can write a native iOS app using Xcode as Integrated Development Environment (IDE) and Objective-C/Swift as languages, taking advantage of the iOS SDK.
A native Android app is written using Eclipse or Android Studio as IDE, Java as language, and the Android SDK as development kit.
A native Windows Phone app is written using Visual Studio as IDE, C# as language, and .NET as Framework.
This is the classic way to develop apps and for some developers, it is still the best way.
Platform specific native development gives some advantages. The first is that we can rely on the OS manufacturer publishing stable updates and we can always be one of the first to use them.
Also, apps written with native tools and languages have performance, security and better user experience advantages. Hybrid apps basically mobile focused web apps built with HTML5 and JavaScript, wrapped in a native container. Native languages and tools (compilers, linkers, and so on) generates more platform specific and natural outputs (binaries). More platform specific binaries more performant apps, generally it means speed.
It also has disadvantages, such as:
In big companies, the platform specific approach works because most of them have a lot of people dedicated to a project and probably they can afford three different teams. Each one of the teams usually works independently to develop the same app in each single platform. The iOS team cannot share a single line of code with the Android team nor the Windows Phone team, or vice versa. This is called the Silo approach.
People usually think about cross-platform mobile development as Write Once, Run Anywhere (WORA approach).
The main advantage of this approach is that we can write something that looks like a WebApp that runs everywhere. There are technologies such as Cordova, Titanium, and others based on WORA. They are all based on the lowest common denominator and can be extendable with plugins to support platform specific features such as NFC or Force Touch, and others.
We generally cannot take full advantage of the features offered by each platform.
Platform-specific features are mostly related to the capabilities of the OS installed and the hardware available in the device. They impact the overall user experience.
Mono is an open source implementation of the Microsoft .NET Framework based on the Common Language Runtime (CLR).
It was initially released in 2004 after three years of open source development launched by a small team of people that included the current founder and CEO of Xamarin, Miguel De Icaza.
Xamarin is derived from Mono framework, which is a cross-platform implementation of .Net Runtime. Xamarin was acquired by Microsoft and open sourced all Xamarin frameworks in February 2016. Microsoft also open sourced .Net Framework and made a cross-platform implementation of it, called .Net Core Framework.
MonoTouch was initially released in 2009 and in 2013 its name became Xamarin.iOS.
Xamarin.iOS is a set of libraries (.dll files) that bind the native iOS SDK.
The iOS binding is the way Xamarin maps the idioms used in Objective-C to the idioms used in .NET.
Mono for Android was initially released in 2011 and in 2013 its name became Xamarin.Android. It is a set of libraries that bind the native Android SDK APIs.
The Android binding maps the idioms used in Java to the idioms used in .NET.
Xamarin.iOS and Xamarin.Android are extendable.
A binding project can be written in order to wrap libraries written in Java or Objective-C into a dll that can be used from our C# projects.
Each single feature of the operating system, third-party libraries, and even our own native libraries can be ported writing a binding project.
The architecture of Xamarin allows us to use the best that iOS SDK can offer to iOS based-devices and the best that Android SDK can offer to Android-based devices.
Xamarin adds another value from the developers' point of view: it allows us to use most of the features of the .NET Framework while developing for Android and iOS devices.
Xamarin.iOS based C# code is compiled using ahead-of-time (AOT) compilation. The resulting compilation output produces a single statically compiled ARM binary.
AOT compilation pre-generates all the native code that the Just in Time (JIT) compiler would normally generate from the Intermediate Language (IL). IL is stripped from the managed assemblies, leaving only metadata. AOT links the metadata together with the JIT-less runtime into a single native binary that can be signed with the Apple account.
An Apple Developer account is needed in order to publish the application to the App Store for iOS devices.
For more information, visit https://developer.apple.com .
The Xamarin.Android application runs within the Mono execution environment (also known as Mono Virtual Machine). The Mono Virtual Machine runs side-by-side with the Dalvik Virtual Machine. Dalvik is an integral part of the Android software stack. It is a process virtual machine in the Android operating system that executes applications written for Android.
The connections between the two virtual machines are created by two Java Native Interface (JNI) bridges:
The managed callable wrappers are generated via .jar binding and are responsible for converting between managed and Android types.
With Xamarin we have different ways to share code.
In general, we want to create a platform specific app writing the most common code we can.
As we can see in the following image, Xamarin.Forms provides shared app logic and shared UI code across platforms. With that support, we can easily develop one true application running and displaying the same on all the platforms.
But there is a thin layer on top of shared layers, and that thin layer adds platform-specific customizations to the Xamarin.Forms projects:
The ways to share the common code are:
