From App Builders CH, Hajan Selmani brings his extensive experience as a Microsoft MVP to show the benefits of writing cross-platform apps with Xamarin. From the high level of lower development costs to the specifics of plugins and API coverage, Hajan gets us started with everything we need to start hacking with Xamarin, and even finishes off with a demo project!
This talk is different than most of the sessions on iOS or Android. It is still iOS and Android (not Windows phone!), but I will present perhaps an unfamiliar approach.
I have been a Microsoft MVP for about five years now, and am CEO and Founder of HASELT (one of the companies that is sponsoring this event as a Gold Sponsor; we are very happy to contribute!) and Hyper Arrow, which is a creative unit inside HASELT that builds promotion materials, videos, marketing and all the creative stuff of the products and services we build.
This talk does not take a deep dive into Xamarin code. Of course I’ll show how to do things so if you want to hack at home you can get started easily, but we’ll focus on the main direction to get started using Xamarin.
The first topic we’ll cover is cross-platform native app development. Cross-platform is not native if you go with hybrids. Bear in mind that some of my arguments may be subjective, but having the experience of working in Microsoft, we can make some predictions. For example even the acquisition of Xamarin, at Microsoft we saw that coming two years ago. It was a matter of time and coming to some maturity level of the development to Microsoft building for cross-platform applications.
It’s very important for me to help people understanding what Xamarin is. What does it mean for you as developers, or anyone who is involved in development (especially for native apps development)? What use Xamarin, and what are the real reasons and benefits for using Xamarin?
We’ll spend some time discussing Visual Studio, .NET and C# for those who aren’t familiar. What does it looks like building iOS, Android and Windows apps with Visual Studio and C#?
Most of you probably do not work with Visual Studio. You only need perhaps less than 30 minutes to set up everything, including the downloading of the HASELT example case, if you have good internet connection. Installing things on Visual Studio and Microsoft platform for iOS and Android goes smoothly and it is on a mature level.
We’ll also discuss tooling, plugins, support, the current state of development and (most important to me) the future. How does Microsoft think about development? If we compare Microsoft, Google and Apple, Microsoft is the only company who mainly works on building development tools for developers.
Xamarin: Is it Really Native? (07:55)
Many people ask me, when they first think about Xamarin, “Is this really native, or is this something in between?” It is native. Microsoft’s vision, Xamarin’s vision, they are going towards that way to help you write native apps using C# and Microsoft tools.
If you are a Microsoft developer, know C# well and work with that framework, the only new thing you need to learn is how mobile apps work.
You always have to learn device-specific things, but the learning curve for native-specific things is not huge. It is worse if you have to learn an entire new language, platform, and tools.
The idea of Xamarin is so simple that Microsoft, .NET and C# developers can start writing code for any device.
We now have the concept of the internet of things, any device can be programmed with an embedded device; you can put software almost everywhere: does it mean that we need to focus more on platform, or things specific to a device (e.g. the fridge), what if next year Google and Apple get a new competitor? We would all learn how to develop with their tools if they adapt some other set of tools, but if we have one single development platform and programming language to target all possible devices, we could skip that.
We focus on one platform, one technology, one programming language, environment, and we target any device. Instead of following the next thing that Apple or Google did, we need to learn how to work with that. You focus on the logic, and building apps. Microsoft will, as much as possible, abstract some of the things, but if you want to go a lower level and work with native stuff (e.g. how you design your forms, or user interface on the mobile device), you can still do it.
You have both options, and sometimes, one or the other might be much better for your business or your applications. Xamarin was quite expensive, but it is now free for everybody who works with Microsoft technology and tools. Why did Microsoft give it away for free? Xamarin had a good business model, it was working fine, they could make money. As a company, Microsoft has changed in the past years – they want to help developers from any platform. But, there is one main point: Microsoft wants you to get on their cloud. Xamarin is fully open source as as part of the .NET foundation, and Mono is also open source.
Why Cross-Platform with Xamarin and .NET? (13:44)
From both the developer perspective and business perspective: why develop cross-platform with Xamarin and .NET?
One programming language to rule them all, on any platform, on any device. You focus on the programming language and the platform as much as possible. Microsoft wants to help you target the device, and just focus on the technology. C# is one of the most advanced languages. When I compare it to Java, there are many things that went into Java a year ago that were available seven or eight years ago in C#, like Lambdas.
C# is focusing on how to make programmers as productive as possible. They are not making overheads with fancy stuff. C# is a strong language, and very mature. If you work with Java, you can quickly get in to C#.
Shared Code Base
You work with one language for three platforms and, if you have a clever way of how you do it, if a bug happens, you can fix it in only one place. You can build high quality apps with a shared code base.
The amount of code that we can share across platforms depends of circumstances and how you build the application. If you are a web-focused developer and go to mobile, web developers tend to write logic in the service APIs, and have more themed clients. Web developers have less reused code on the client – they would be more platform-specific. If you are mobile app developer, you can quickly see that the code that you use and reuse in many applications has code that you have used in many applications, you probably now put it in one place. And you have it, not only for Android, but for iOS, Windows, other platforms.
Microsoft and Xamarin helps you have control over API through all platforms. You have the SDK, and all the platform code source code, you can use libraries imported from any platform as part of your application. They support everything that is supported in your Android SDK, or your iOS SDK, etcetera.
Lower Development Costs
In practice, in the past few years with Xamarin, development costs were not lower, but now it is lower development costs. In our company, and the companies we cooperate with that also use Xamarin cross-platform with this style of development, we do estimations for building an app for different platforms.
Right now, the development cost is 30% lower with Xamarin. If you have a good team that writes code well in C#, understands .NET, and knows how mobile dev works in Android or iOS and other devices, it can speed up your development and lower your development costs. It will depend on many scenarios: for example, if you build an application, then it has specific things on all platforms, and they differ by the platform or device-specific requirements, then maybe development costs will not change much. But probably more than 50% of applications do not have very fancy features. Xamarin tries to help you abstract even more libraries and plugins and everything you need that helps you smoothly develop for all platforms.
.NET is simple, powerful and native; you do not need to think about any other platform.
- It uses C# language, and mono .NET framework.
- Compiler: Microsoft uses Roslyn compiler as a service. They try to use it in most of the cases. Roslyn helps on runtime, and does magic stuff that gets stated in the native platform that you are targeting.
- For the IDE Tools, you can use Xamarin Studio or Visual Studio. I would recommend using Visual Studio because, since Microsoft acquired Xamarin, they will force more ongoing Visual Studio updates (Xamarin Studio may even get depreciated after some time, because why do they need to develop environments or IDEs?).
Visual Studio Under the Hood (22:47)
Visual Studio already supports almost everything Xamarin Studio has. But how does it work under the hood?
C# has advanced futures, but there might be some limitations. For example, you cannot use dictionary if you write for iOS, as it will probably crash your application. Visual Studio will help you because it will tell you, “This is depreciated, you should not use it in the context of iOS, because there are some issues in the compilation.” With iOS, you usually work with iOS servers (you have to have an iOS device). With Visual Studio you have a full interface of connecting remotely to devices.
With Android it works similarly: it works natively in Windows phone, it uses virtual MonoVM + JIT’ing that it gets from the compilation. If you have a lot of code written for different purposes, Visual Studio helps you optimize your code by stripping out the non-used code. You get a smaller application that does not put all the code as a compile.
Platform SDK Access (24:50)
The platform for Apple is CocoaTouch SDK, and Android SDK for Android. For Windows phone it works by default, natively. It exposes all the APIs. You can type using any name space that you have been using already if you have been developing for Android or for iOS, and you can access them in C#.
IDE Availability (25:27)
If you choose to develop for iOS, Android, Windows Phone or Windows apps, with Visual Studio all of these are automatically supported with Xamarin Studio (only Windows 4 Phone is not supported). On MAC OS, and not on Windows OS, it is only Android for iOS, you again need to have a MAC device in order to be able to emulate and test.
Platform APIs (26:00)
Windows library APIs just work on top of all the C# libraries and namespaces – it just adds them on top of the .NET and C# framework.
iOS – 100% API Coverage
It works the same for iOS: it has 100% coverage. When they release the new version of the SDK, Microsoft will import this and it will work the same. It is very important for them to give 100% availability and access to the APIs. Before you build the first application for the new platform, with the new SDK or the new device, it will already have the APIs.
Android – 100% API Coverage
For Android it is also the same; there are differences in SDK because they are platform specific.
Xamarin’s Cross-platform approach (27:26)
Why do I need to care about which platform is which? I just want to build applications, and if I have a feature that needs to work this way or that way - I do not always need to know what is under the hood of the device.
When you write cross-platform applications with Xamarin, you have a shared C# logic. When you plan your application, you plan a number of features. The idea is that you focus all this logic, and you can put it in a shared source project. You write it once, and it works on all devices.
For the UI there may be differences in the platforms. You have different products for each platform, and then design and work with the UI or write the code that is correlated with the UI there.
Sharing Code - Strategies (28:20)
Shared projects and portable test laboratories, both have pros and cons. With shared projects, you create shared code and if you want to share the code (the source code, not the DALs, the compiled code), then it is good to go with shared projects.
For example, if you are three developers working on the same solution, and you have shared source code, maybe you have a developer who works with C#, but building the app for iOS. In portable class libraries, this is a matter of designing it how you want it. It is a good way if you write a source code, and then you want to reuse it in many other projects in the future. After some time you find some patterns and practices that you can put into class libraries, and as APIs that you can then use for all your future projects.
Plugins for Xamarin (29:34)
There are several plugins available. For many custom scenarios that are not straightforward, they are developing more plugins. They also use native APIs and common mobile APIs from Xamarin. They access different device features and capabilities.
You can create the UI the same way you would create UI on iOS or Android, use the source code written in C#, and build the application. But Xamarin has one more way in which you can create UI: Xamarin.Forms.
If you do not have many differences in the app that you want to achieve as a final product, Xamarin.Forms is a good solution. Let’s say you build an app, and there are not many platform-specific differences. Sometimes they look the same both in iOS and Android. With Xamarin.Forms, they abstract and give you one more way to reuse shared code. You can build forms in Xamarin, in the shared project, and it will work on all platforms. There are multiple examples with Xamarin.Forms to follow too.
Xamarin Test Cloud (31:41)
There are many devices in the cloud for different platforms to test your app on different devices, different versions, etc. It has some excellent dashboards you can look at and see many things happening on the device while testing.
There are other things that are available on Xamarin. Because Microsoft acquired Xamarin, there will be many new things happening in this field of cross-platform development. If you start in Visual Studio and want to build an application for iOS or Android, you do not even see Xamarin, you only see cross-platform. You do not think about what it is, you just build cross-platform apps that work fully natively.
Inspector and Workbooks (33:29)
They have built additional tools, beside the main Xamarin studio SDK: inspector and workbooks. They have also built Xamarin emulator for Android, which is very fast, and you can even configure to make it work almost instantly.
Inspector and workbooks is an additional tool that can help you inspect on the fly, or test things with your application. This uses the Roslyn compiler as the service behind it. You can run your application, type any code, import any library from iOS, Android, Windows Phone, or Windows application. Then, you can test on the fly and see what is going on with your device.
With workbooks, you can create different workbooks for different scenarios, and the code that you write is in C#.
I will showcase how this looks in Visual Studio, and how you can start building your application.
If I want to create a new project: file, new project. You have different templates for different usages: I can go cross-platform, and select some of the templates that are suitable for my case. If I want to write the software in C# and .NET but I want to build the UI in Xcode or XML in Android or iOS, you can go with the native approaches. If I want to go with Xamarin.Forms, I can use one of those templates.
I mentioned the difference between portable and shared: it does not matter in this case, I will just use shared. It will create my project, and you will see that it will return an error because I do not have the SDK for Windows Phone (I do not develop for Windows Phone, I did not need to use the SDK). You can see I have code specific to Android, and I have code specific to iOS, Windows, and specific to Windows Four, I have the SDK. If I have some third platform, web for example even, you can add it further and they will of course ad support soon.
If I click on Android, you will see that I have the emulators for Android. I have other virtual devices - with Xamarin Android player. I can install any device I want immediately here, and then I can use it. If I click on iOS, you will see that it needs a device. I can very quickly connect through tools to Xamarin Mac agent, and then it will ask me for remote login. You should enable remote login on the device, and then you are already set up. If I click on Windows, it will start from the login machine.
I will start with Android. The first build should be 10-15 seconds. It will start the Android emulator for the default selected. It will run the operating system and we start the application. The interesting thing is that I can write source code, and I do not need to close the emulator. I can write source code, build it, refresh the emulator and see the differences immediately. I do not need to wait every time (30 seconds or up to a minute) for the emulator to settle.
Here is the the application; it is a simple one, and I will add a button. I will build this and start from the emulator. The UI is programatically built here, I can build it with Xamarin if I use forms. I have the button - I can add a click event on the button in C#. Simple click. It works: it is an Android app with C#. I could start with iOS, if I had the device, and I can start it with Windows. It’s the same code, you do not need to do anything differently.
In Xamarin, if I use Xamarin.Forms, I can quickly add a form item for showcase. For example, if I go over to iOS, and click “add new item”, it will show me a storyboard and how everything works in iOS. If I go to Android and click “add new item”, it will show me activities, environment.
You can also work with the designer in Visual Studio. If I go in shared code, it assumes that you choose Xamarin.Forms, I can go cross-platform and there is a content page, view page and XAML if I want to write an entire code in XAML, XML but with some conventions. I can add a content page. If I go over the label, it shows me a label - is not available in Android, it is not label. I will add a new list view, add item source (shrink array of a few elements). I will also add, iOS, Android, Windows on the the stack layout. I want to make this a main page; I do not need to add any navigation elements. The main page, new page one. I am on Android - I will just start on the Android emulator. In parallel, I will start with Windows. Now it is deploying on a Nexus.
I made a simple list: you can bind events to elements, you can decorate them. You can have this immediately available. If I start this with Windows, I have the list on the Windows app, and it is fully the same source code. There are many different things that you can try. 100% of the controls are already available through Xamarin.Forms. You can use them today, and many plugins that are already built with this.