Mobile Development with Xamarin
The course is part of this learning path
Xamarin is software that enables you to develop an application once and distribute it on multiple platforms with minimal or no alteration to the codebase. This course introduces you to Xamarin and you'll learn how to set up a development environment, as well as what's needed to develop apps for different platforms. We'll also run through how to create a basic app and deploy it to Android and iOS.
- Learn how Xamarin's architecture tackles sharing a codebase in a cross-platform scenario
- Understand how to set up a Xamarin development environment on Mac and Windows machines
- Learn how to build and debug simple Android and iOS applications
This course is intended for anyone who wants to learn how to use Xamarin to develop mobile applications for Android and iOS.
There are no prerequisites for this course but any familiarity with programming concepts like design patterns would be helpful.
As you can build iOS apps with Xamarin, it's only fair that you should be able to run Visual Studio on a Mac. Which you can, along with the .Net framework. Let's start by installing Visual Studio on a Mac running Monterey. I'll be doing most of the demos on Windows, but to keep the environments as consistent as possible, if that is at all possible, I'll install Visual Studio 2022 on the Mac even though it is a preview version. Yep, this is a risky strategy, but I'm somewhat of an early adopter – when it's free. We download the Visual Studio Installer from Microsoft, which will manage the installation and any updates or configuration changes we might want in the future. Setting up Visual Studio first involves selecting the components you want to install. There are only three optional components, and the first difference I can see from Visual Studio 2019 is Andriod isn't selected by default and is marked as "Legacy." For consistency's sake, I will install the Android components. Let's come back to that when it's done. We'll need to have an up-to-date Xcode IDE installed that Visual Studio and Xamarin will use to compile the iOS version of our app and run it in an iOS simulator, so I'll let that run in the background.
Right, Visual Studio has started; I ticked the start when the installation is complete checkbox, and just a few more permissions to grant to get underway. The splash screen has picked up an existing .NET project I have, and over on the right, and we can open or create a new project from pre-configured solution templates. We want to create a multiplatform app. Below are sections dedicated to each platform, iOS and Android. Under Xamarin Forms are templates for three common app layouts. The blank app template allows us to use either C# or F# to write our logic code, while the other two are C# only templates. While Visual Basic is a .NET language, it's not currently supported for cross-platform development. I'm not sure why F# is only a choice for the blank app template. I don't believe it is a technical reason, as you could easily start with a blank template and then add flyout or tabbed functionality. F# is a relatively recent addition to the .NET suite of languages, with C# being the most popular. I suspect it's more of a case of "look, you can even build a mobile app with something other than C#." As I said before, these are Xamarin forms apps as opposed to Xamarin native. Just keep that in the back of your mind for the moment, as I'll talk about the difference when we get into building our first app.
Let's go with a blank app using C#, which I'll call iosdroid. I'll call my company xamintro and target android and iOS. Click next, and I'll stick with the defaults and click create. Okay, here we have our iosdroid solution containing three projects. The iosdroid project is the Xamarin project, where you design the screens for your app and write the code. Below that, we have the iosdroid.android project and the iosdroid.iOS project. These projects contain the infrastructure and scaffolding required for each of the target platforms. The android project is in bold, as Visual Studio has set it as the default project when running the solution in debug mode. While the Xamarin project is where all the code writing will happen, it can only run in the context of either an android or iOS app. Strangely, Visual Studio has made the android project the default on macOS. It could be that android comes before iOS alphabetically or that the supporting Xcode version and libraries are incompatible with the installed version of Xamarin, as denoted by the warning icon. Once the new version of Xcode has finished installing, that warning will disappear.
Mainpage.xaml is the main or opening screen of the application. If you are unfamiliar with xaml, which stands for extensible application markup language, it's a little bit like a hybrid of XML and HTML. ContentPage is analogous to the body tag of an HTML page, while StackLayout and Frame are container controls, like a div. Inside the containers, we can see tags for labels with text attributes. As you may have guessed, xaml is a Microsoft language used in other technologies such as Windows presentation foundation. Expanding MainPage.xaml, we can see MainPage.xaml.cs. This is called the code-behind file in Visual Studio, where the C# logic related to the presentation xaml file is written. App.xaml is the entry or start point of the application. The xaml file doesn't contain anything interesting in a simple application like this. In the code behind cs file, we can see the creation of a new MainPage instance within the app's constructor that will start the application.
Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard.