Xamarin Forms and .NET Standard - Getting Started
Learn about the advantages of Xamarin Forms for cross-platform app development and see how to get started using it with .NET Standard.
Join the DZone community and get the full member experience.Join For Free
In this blog post, we're going to lay the basis for future posts about Xamarin Forms. We'll explore briefly what's Xamarin Forms and why it's so powerful. Then we'll talk about .NET Standard and we'll understand why it's a very big achievement for the .NET ecosystem.
Xamarin Forms is an API to develop cross-platform apps for iOS, Android, and Windows from a single shared C# codebase. It's not a one-size-fits-all technology, but it's very useful when code sharing is more important than a custom UI for every platform. If we are Windows developers, we'll love Xamarin because it allows us to reuse all our hard-earned skills like XAML, binding, MVVM, and so on. If we need device-specific features, we can access them, too.
The .NET Standard is a formal specification of .NET APIs that are intended to be available on all .NET implementations. The motivation behind the .NET Standard is establishing greater uniformity in the .NET ecosystem, enabling developers to produce portable libraries that are usable across .NET implementations, using this same set of APIs.
We can look at .NET Standard as a specification of .NET APIs that make up a set of contracts. These contracts are implemented in each .NET implementation. This enables portability across different .NET implementations, allowing our code to run everywhere.
A Fresh Start
Before getting started, make sure you've run the Visual Studio Installer and have upgraded to the latest stable release of Visual Studio. At the time of this writing, I have this setup:
The first step to creating a cross-platform Xamarin Forms app is to create a new project with Visual Studio.
- Visual C# -> Cross-Platform
- Cross-Platform App (Xamarin.Forms)
- We give a name to the Project (GettingStarted) and press OK.
When we press OK, a new window appears. We choose blank-app as a template, Xamarin.Forms as the UI Technology, and .NET Standard as the Code Sharing Strategy (read here about Shared Projects).
When we hit OK, Visual Studio works for a while and prepares a new solution for us.
Anatomy of a Cross-Platform Solution
The new solution is composed of four projects:
- The shared code project (red rectangle). The output of this Project is a dll that can be used in every implementation of the .NET Standard version of our choice (.NET Standard 2.0 is the default setting). We want the big part of our code inside this Project to minimize differences between the three flavors of our app.
- The platform-specific projects (Android, iOS, Windows). The output of these projects is the app for the specific target Platform. In these projects we implement specific functions/UI/services for the target device; to start a phone call, for example, the code is different for every OS and we need to write our code for each platform and not in the shared Project.
To check that everything is ok we try to build and run the Android version of the App in an emulator. We set the Android Project as the Startup Project with right click in the Solution Explorer and then Set as Startup Project.
We choose our favorite Android Virtual Device (AVD - I created a custom configuration) and then we press Play/Run.
After compiling and launching the AVD, Visual Studio will deploy and run the app.
We can do the same thing with the other two device-specific projects. Keep in mind that to build the iOS Project you need a Mac on your network.
In this post we created a Xamarin Forms Project with the Visual Studio template, we learned the purpose of Xamarin Forms and .NET Standard. Now we have the basics to develop the app of our dreams.
In the future posts, we'll evolve this app introducing MVVM and other concepts.
- Xamarin official website ( https://developer.xamarin.com/)
- Microsoft documentation ( https://docs.microsoft.com/en-us/dotnet/standard/net-standard)
Published at DZone with permission of Michele Ferracin, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.