© Alessandro Del Sole 2017

Alessandro Del Sole, Beginning Visual Studio for Mac, https://doi.org/10.1007/978-1-4842-3033-6_4

4. Introducing Xamarin

Alessandro Del Sole

(1)Cremona, Italy

Xamarin is, at the same time, the name of a company that Microsoft acquired in February 2016 and the name of a suite of products and services Microsoft offers to developers to build native mobile applications for Android, iOS, and the Universal Windows Platform using C# and the Microsoft developer tools.

This chapter introduces Xamarin by discussing how it fits into the mobile-first , cloud-first vision at Microsoft and by listing the available products and services, with a brief explanation of what you can do with each.

Note

In this chapter, I will often mention Windows 10 as one of the platforms that Xamarin supports. I will provide these mentions for the sake of completeness, but keep in mind that Visual Studio for Mac supports only Android, iOS, macOS, and tvOS. If you want to target the Universal Windows Platform in your Xamarin solutions, you must use Visual Studio 2017 on Windows.

Considerations About Cross-Platform App Development

For a better understanding of how Xamarin can solve many problems and why you should really take this technology into consideration, I will discuss the way companies build apps and the problems they face, especially with a background on the Microsoft stack.

In the past few years, we all have seen an incredible growth in the proliferation of mobile devices, not only smartphones and tablets but other kinds such as wearable devices, smart TVs, and Internet of Things (IoT) devices. All of these kinds of devices are powered by the most popular operating systems, such as iOS (Apple), Android (Google), and Windows 10 (Microsoft). With this variety of devices and operating systems, a company that wants to increase its business by offering an app to its customers should publish a version for each platform. This has a number of important implications.

  • Different operating systems rely on proprietary APIs. These are completely different from one another and require completely different ways to be handled.

  • The major producers (Apple, Google, and Microsoft) have their own developer tools, frameworks, and programming languages, which are completely different from one another. Apple provides Xcode with Swift and Objective-C, Google provides Android Studio based on Java, and Microsoft provides Visual Studio and C#.

  • Different operating systems also have their own infrastructure, rules, and settings for the user experience and the user interface, which require knowledge of the mechanisms that are behind the scenes of the UI on each operating system.

As you can imagine, if a company wants to build and publish one or more apps to the Apple Store, Google Play, and the Microsoft Store, it needs a number of developers with different skills and expertise. This is certainly not uncommon for many companies, but it can be a problem if a company has always worked only with Microsoft technologies, frameworks, platforms, and languages, such as .NET and C#, when it also needs to be in the iOS and Android markets. In this situation, a company has typically two alternatives: hiring iOS and Android developers or spending the time and money to make their developers skilled and productive on the Apple and Google development platforms. In both cases, the company will have to face potentially huge costs, and only in the first case might it save some time. So, the ideal situation would be having an option to write apps for different operating systems reusing developers’ existing skills on C# and .NET and sharing as much code as possible. This is where Xamarin comes in.

Understanding Xamarin and Its Objectives

Xamarin has the goal of providing developers with the tools they need to build apps for the most popular operating systems for devices (iOS, Android, Windows 10) using C#. The biggest benefit is that developers can reuse their existing skills with this language, and they can target multiple platforms by maximizing code reuse.

To make this possible, Xamarin takes advantage of the Mono framework ( www.mono-project.com ), an open source, cross-platform porting of .NET that runs on macOS, Windows, and Linux (and its various distributions) and whose development was started by two founding members of Xamarin, Nat Friedman and Miguel de Icaza. Mono supports the C# and F# programming languages and takes care of exposing platform-specific APIs in the form of .NET namespaces and types. Over the years, Mono was ported to mobile platforms as well, including iOS and Android. Put succinctly, Mono and C# are the foundations for building cross-platform, native mobile apps with Xamarin. This would not be possible without C# wrapper libraries that expose each operating system’s APIs in the form of .NET types through Mono. Table 4-1 summarizes the available libraries.

Table 4-1. Xamarin Libraries

Library

Description

Xamarin.Android

A wrapper for the Java framework that allows building native Android apps in C#, including apps for wearable devices and smart TVs

Xamarin.iOS

A wrapper for Apple’s frameworks that allows building native iOS apps in C# for the iPad, the iPhone, and the Apple Watch

Xamarin.Mac

A wrapper for Apple’s frameworks that allow building native applications for macOS in C#

Xamarin.TVOS

A wrapper for Apple’s frameworks that allow building native apps for tvOS devices in C#

Xamarin.Forms

A library that allows writing cross-platform apps in C# by sharing the user interface and all the code that is not platform-specific

As you can easily imagine, Xamarin.Android targets only Android devices, whereas Xamarin.iOS, Xamarin.Mac, and Xamarin.TVOS target iOS, macOS, and tvOS respectively. If you want to write code once and generate apps that run on Android, iOS, and Windows 10, then you can leverage Xamarin.Forms, a unified layer that allows sharing the user interface definition and code that is not platform-specific. Xamarin.Forms is described in more detail in Chapters 7 and 8. As I mentioned at the beginning of this chapter, Xamarin not only offers development tools but also includes a number of services. In the next subsection, I will provide more details on what Xamarin offers.

The Xamarin Platform

The Xamarin platform provides the development tools you use to build cross-platform apps using C#. More specifically, the Xamarin platform includes all the libraries described in Table 4-1, plus the following tools:

  • Visual Studio for Mac, the integrated development environment that runs on macOS.

  • Extensions for Visual Studio on Windows, which make it possible to use the premiere development environment from Microsoft to create Xamarin solutions. Both Visual Studio 2015 and Visual Studio 2017 include Xamarin extensions out of the box, and they can be installed separately on Visual Studio 2013.

  • Xamarin Profiler, a new tool that allows you to analyze memory usage, CPU consumption, and where an app spends the most time. Therefore, this tool is precious to understand where and how you can improve performances.

  • Xamarin Inspector , a new tool that allows for analyzing the behavior of the user interface of an app at runtime.

  • Xamarin Workbooks, a playground that makes it easier to learn C# and the .NET/Mono libraries.

  • Xamarin iOS Simulator for Windows, a simulator that runs locally on Windows machines but that is available only for Visual Studio 2015 and 2017 Enterprise.

With this powerful set of tools, you can quickly build cross-platform native apps reusing your existing C# skills. Hints about using the Xamarin Profiler and Xamarin Inspector will be provided in the next chapters.

The Visual Studio Mobile Center

Note

If you are already familiar with Xamarin and its services, you might know about Xamarin Test Cloud and Xamarin Insights (with HockeyApp). Both are transitioning into the Visual Studio Mobile Center, and Microsoft says the transition should be completed by the end of 2017. This is why you do not find information about them in this chapter and why I will talk only about the new portal.

The Visual Studio Mobile Center ( http://mobile.azure.com ) is a new portal from Microsoft that allows developers to fully manage the whole application life cycle for mobile apps. It is hosted on the Microsoft Azure platform, and it offers services that cover the application life cycle with build automation, test automation, analytics and crash reports, distribution to testers, and back-end services such as tables and authentication, based on a DevOps approach. Figure 4-1 provides a sample view of the Visual Studio Mobile Center where you can see all the available services on the left, based on an Android app written with Xamarin.

A449551_1_En_4_Fig1_HTML.jpg
Figure 4-1. A sample view of the Visual Studio Mobile Center

To use cloud-based services from the Mobile Center in your Xamarin projects, you simply need to install some NuGet packages that contain portable .NET libraries that make it really easy to consume back-end services and to implement analytics. Discussing these libraries is beyond the scope of this book, but when you associate an application to the portal, the Visual Studio Mobile Center will display the list of NuGet packages you need and how to use them.

Actually, the Visual Studio Mobile Center is not limited to work with apps created with Xamarin; it also supports other proprietary languages and tools. More specifically, all of its services can be used against Android apps built with Java, iOS apps built with Objective-C and Swift, and apps built with React Native. The following paragraphs describe all the services provided by the Visual Studio Mobile Center.

Back-End Services

Note

Behind the scenes, the Visual Studio Mobile Center relies on the Azure Mobile App back-end services that you can also control from the Azure Portal ( https://portal.azure.com ).

Most mobile apps need back-end services, for example, for storing data and for authenticating users. The Visual Studio Mobile Center offers table services and identity services in the cloud. An important note is that both require you to have a Microsoft Azure subscription (trial subscriptions are also acceptable).

With tables, you can quickly set up a data store based on unrelated tables with rows and columns. With the identity service, you can quickly implement authentication based on the most popular providers, such as Microsoft, Facebook, Twitter, Google, and Azure Active Directory . You can combine the two services to restrict access to tables only to authenticated users. The biggest benefit of using the Mobile Center for back-end services is that it dramatically simplifies the way you create tables and how you implement authentication, especially when compared to how you configure both in the Azure Portal.

Analytics and Crash Analysis

With one line of C# code, you can enable analytics in your Xamarin projects and make the results available in the analytics service of the Visual Studio Mobile Center. With this service, you can review detailed information on app crashes but also on the app usage.

For instance, you can view the number of downloads; the number of sessions and of active users; countries where your app is downloaded the most; and information about the culture, language, and region of devices where your app is installed. Figure 4-1 shows an example of how the Visual Studio Mobile Center provides this information.

Build Automation

You can configure build automation for continuous integration by connecting the Visual Studio Mobile Center to any Git repository. This way, every time you commit or push changes to your source code, the service will automatically generate a new build. You can then certainly get detailed information about successful and failed builds.

It is worth mentioning that the Visual Studio Mobile Center has its own Mac agent for builds, and this potentially avoids the need of having a networked Mac when compiling Xamarin solutions for iOS. At this writing, the build automation service can connect to GitHub, Bitbucket, and Visual Studio Team Services as the hosts of Git repositories.

Test Automation

The test automation service allows you to execute automated tests against your apps, including UI tests. The Visual Studio Mobile Center allows you to execute tests on more than 2,000 devices and 400 different configurations. This service is particularly useful because it allows you to fully test your app in a variety of conditions before you ship it to customers.

Distribution to Testers

With the Visual Studio Mobile Center, you can easily send your app package to the specified testers once the automated tests pass. Testers will receive the app directly on their devices, and you will be able to view the analytics and telemetry information in the portal. This service is based on the HockeyApp engine, which Microsoft acquired.

The Xamarin University

The Xamarin University ( www.xamarin.com/university ) is a web site that offers training materials and courses, including live classes with instructors, where you can find everything you need to learn Xamarin and to increase your skills, expertise, and productivity.

The Xamarin University is important because it allows you to prepare yourself to get the Xamarin Certified Mobile Developer certification. This requires you to complete a number of classes and exams. Additionally, the Xamarin University occasionally publishes free webinars and online technical sessions.

Summary

In this chapter, you took a closer look at Xamarin, getting more detailed information on what it is and on how it fits into the mobile-first, cloud-first strategy at Microsoft.

In this chapter, you saw a number of considerations about the different approaches to building mobile applications for different OSs with proprietary technologies. Next, you saw how Xamarin solves most of the problems by providing tools that allow developers to build mobile apps with C# in a cross-platform way. In this discussion, you got information about the available libraries and development tools. Because Xamarin consists not only of developer tools, this chapter also provided a high-level overview of the complete Xamarin offerings, which includes the Xamarin platform with development tools; the Visual Studio Mobile Center, which is based on the DevOps approach and that offers services for back-end, analytics, build automation, test automation, and distribution to testers; and the Xamarin University, which is the place where you can learn and attend classes that will lead you to the official Xamarin Certified Mobile Developer certification. After this short but necessary introduction to Xamarin, in the next chapter you will start creating Xamarin projects, and the first platform you will target is Android.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.244.7