Lesson 1 - Introduction to Xamarin
Welcome to the course on developing cross-platform mobile application in Xamarin and C# .NET for beginners. This course assumes a basic knowledge of C# .NET. You're going to learn how to create native Android, iOS, and Windows apps. We'll start with a classic Hello world application, then we'll get to using the database, camera, contacts, notifications and retrieving data from a website using an API.
Introduction to Mobile Applications
Mobile apps, like web apps, are on the rise today. Let’s think about why these applications are so popular for both users and businesses. It's really simple.
If the user installs our application, we can inform them about news, benefits, etc. easily and directly on their phone and thus increase the chance to sell something. This would probably be difficult to achieve with an email newsletter, which the user usually deletes straightaway without reading it at all. As a bonus, we'll get information about the user's device, such as their location, so we can target our offers to a specific city / location and adjust the product offer accordingly. That was for businesses. And for the mobile users, the benefits of mobile apps are that they can browse most of the content offline, for example, when traveling by subway.
Sounds good, doesn't it? Let's also move to the pitfalls of mobile application development. There are currently 3 platforms (Android, iOS, and Windows) and each uses a different programming language.
- Android (Java, Kotlin)
- iOS (Swift)
- Windows (C#)
So if our customer wants to create an application for all these platforms, it'd be a hell of work. If you ever wondered it's unnecessary to write the same application 3 times - one for Android, the other for iOS, the third for Windows, and each in a different language, then you're in the right place.
What is Xamarin?
Xamarin allows to develop native applications for all the platforms at once, with a minimum of OS-dependent scripts and all using only C# .NET and XAML. So we can share about 90% of common code across all platforms. Haven't you heard of XAML yet? Never mind. As the name suggests, it's a classic XML with extended syntax (eXtensible Application Markup Language). We'll use it to design the visual layout of our applications, which we'll then "bring to life" with C# scripts.
And how does it actually work? As I mentioned earlier, it's kinda revolutionary that Xamarin development is native and in C# .NET at the same time, even for platforms with different native languages. The principle is again very simple. When we build the project, it's compiled to the native subsystems (Java for Android, Swift for iOS) and each application then looks and behaves as if it was written in the native language for the platform. Certainly now we all see the major advantages of Xamarin. Another indisputable advantage is that if we don't know Java or Swift and want to create even an application just for Android or iOS, we can do it in C#. Xamarin offers 5 basic solution types:
- Xamarin.Forms (development for all platforms at once)
- Android application
- iOS application
- Android Wear application
- WatchOS application
So we can choose what we need comfortably. In this course, we'll focus mainly on the development using Xamarin.Forms. I'm sure you'd be able to use the other solution types if needed.
Note: To test iOS applications, you need to work on a MAC or have an iOS device paired with your PC. You won't be able to test iOS versions without that.
Development Environment Installation
As usual, we're going to need an IDE. Since we're dealing with a Microsoft technology, no one will be surprised that it's going to be Visual Studio. I'm going to use the Community version. You can download it at https://visualstudio.microsoft.com/. If you have a different version, there may be minor differences.
When you finish downloading, run the installer. The installation page with tools will load, check what you need + Mobile development with .NET. Check "Android SDK setup" on the right. If you want to create applications for Windows Phone, you need to check UWP (Universal Windows Platform). Then click "install".
The installation will take some time depending on the selected features and your connection speed. At a minimum, Visual Studio has about 10GB.
Preparing the Development Environment
After starting Visual Studio, select "Create new project" and "Mobile App (Xamarin.Forms)" on the next page.
On the next tab, we'll select "blank" and click on "create". Although we chose the blank project, Visual Studio generated an entire directory structure for shared code, Android and iOS, and if you installed UWP, for Windows as well. You can see the structure in Solution Explorer on the right.
Before we start analyzing the structure, it's necessary to install several additional components. We'll click on Tools -> Android -> Android SDK manager. A window with two tabs (Platforms and Tools) will open. On the Platforms tab, we'll check everything from version 7.0 and click "Apply Changes". Developing for older Android versions won't pay off anymore. Moreover, when we'll publish our application on Google Play, Google will require a minimum API that is 3 years old at most. If we build our application on an older API, we'd have a problem. Another advantage of using newer APIs is the availability of more modern techniques and more functions, graphical accelerations, etc. The appropriate selection of the API is an important part of the success of the whole application. If we build everything on the latest version, 80% of people won't even launch our app. When we build it on a really old one, uncle Google will slap us on the wrist. Therefore it's a good idea to choose the middle ground. We'll talk more about this in the next lessons.
Let's go to the second tab - Tools. There we'll install Android SDK Build Tools and Android Emulator. Over time, we'll also install NDK and a few Google libraries, but now it's not necessary.
Now we have the environment ready. If you did everything right, Visual Studio should look something like this. Note the Warnings window in particular. If you're getting any SDK warnings, just restart Visual Studio.
That'd be all for today. In the next lesson, Xamarin Project Structure and the Application Life Cycle, we'll talk about what happens in the application from the start, through when it's paused, to the termination, discuss the methods, and examine the Xamarin project structure.