Creating your first Xamarin application
As a developer planning to create native mobile applications using Xamarin, you have several options for setting up your development environment. In terms of development, both macOS and Windows can be utilized, using either Visual Studio or Rider IDEs.
As a .NET developer, if you are looking for a familiar environment and IDE, the best option would be to use Visual Studio on Windows.
In order to use the Xamarin-related templates and available SDKs, the first step would be to install the required components using the Visual Studio installer:
When you install the Mobile Development with .NET component, the required SDKs (for Android and iOS) are automatically installed so that the developers don't need to do any additional prerequisite installation.
Once the setup is complete, various project templates become available under the Cross Platform App section, as well as platform-specific sections, namely Android and iOS. The multi-project template for the cross-platform Xamarin app is to guide you through the project creation process using Xamarin.Forms, while the available Android App and iOS App templates create application projects using the classic Xamarin infrastructure:
Using this template, Visual Studio will create a common project (shared or .NET Standard) and a project for each selected platform (selected platforms out of iOS, Android, and UWP). For this example, we will be using the Shared Project code sharing strategy and selecting iOS and Android as target platforms.
In this solution, you will have platform-specific projects, along with the basic boilerplate code and a shared project that contains the Main.xaml file, which is a simple XAML view. While the platform-specific projects are used to host the views that are created using the declarative XAML pages, the MainActivity.cs file on an Android project and the Main.cs file on an iOS project, are used to initialize the Xamarin.Forms UI framework and render the views.
In the Main.xaml file, the code that is used to create a label and center it both vertically and horizontally would look similar to the following:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:FirstXamarinFormsApplication" x:Class="FirstXamarinFormsApplication.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
This XAML view tree is rendered on the target platforms using the designated renderers. It uses the page, layout, and view hierarchy: