Introduction
Xamarin allows developing native mobile applications for iOS and Android using C# and .NET. This approach reduces maintenance costs through code sharing. In 2026, Xamarin.Forms remains relevant for intermediate projects requiring a declarative UI. This tutorial guides you step by step to a functional application with navigation and data binding.
Prerequisites
- Visual Studio 2022 with Xamarin workloads
- .NET 6+ and Xamarin.Forms 5.x
- Basic knowledge in C# and XAML
- Apple developer account and configured Android SDK
Creating the Project
dotnet new Xamarin.Forms -n MonAppMobile
cd MonAppMobile
dotnet restoreThis command initializes a Xamarin.Forms cross-platform project with the Shared, Android and iOS folders. The restore downloads essential NuGet dependencies like Xamarin.Essentials.
App.xaml Configuration
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MonAppMobile.App">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="Button">
<Setter Property="BackgroundColor" Value="#2196F3" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>App.xaml defines global resources. The style applied to buttons ensures visual consistency across all platforms without code duplication.
Main Page in XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MonAppMobile.MainPage">
<StackLayout Padding="20">
<Entry x:Name="NomEntry" Placeholder="Your name" />
<Button Text="Submit" Clicked="OnValiderClicked" />
<Label x:Name="ResultLabel" FontSize="20" />
</StackLayout>
</ContentPage>This XAML file creates a simple interface with an input field and button. The names allow referencing from code-behind for event binding.
Page C# Logic
using Xamarin.Forms;
namespace MonAppMobile
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void OnValiderClicked(object sender, EventArgs e)
{
string nom = NomEntry.Text;
ResultLabel.Text = $"Hello {nom}!";
}
}
}The code-behind handles the button event and updates the label. This XAML/C# separation facilitates maintenance and unit testing.
Data Model
namespace MonAppMobile.Models
{
public class Utilisateur
{
public string Nom { get; set; }
public int Age { get; set; }
}
}This POCO model will be used for data binding. It follows naming conventions for automatic binding in ListViews.
Binding and ViewModel
using System.ComponentModel;
using Xamarin.Forms;
namespace MonAppMobile.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
private string _nom;
public string Nom
{
get => _nom;
set { _nom = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}The ViewModel implements INotifyPropertyChanged for reactive binding. This allows the UI to update automatically without manual code.
Best Practices
- Use MVVM to separate business logic from the UI
- Manage permissions with Xamarin.Essentials
- Test on Android emulators and iOS simulators
- Optimize images with FFImageLoading
- Regularly update NuGet packages
Common Errors
- Forgetting InitializeComponent() in the constructor
- Not implementing INotifyPropertyChanged correctly
- Ignoring rendering differences between iOS and Android
- Using blocking calls on the UI thread
Going Further
Deepen your Xamarin knowledge with our advanced modules on Shell navigation and REST API integration. Discover our Learni trainings.