100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

XAML Basics

The essentials of MAUI's XAML syntax — layouts, controls, data binding, and markup extensions — for declaring a page's UI separately from its code-behind logic.

FoundationsBeginner9 min readJul 10, 2026
Analogies

XAML Basics

XAML (Extensible Application Markup Language) is a declarative XML-based syntax for defining a MAUI page's UI tree — layouts, controls, and their properties — separately from the C# code-behind that handles logic and events. A XAML file like MainPage.xaml is paired with a code-behind file MainPage.xaml.cs via a partial class and the x:Class attribute, and the XAML compiler (XamlC) compiles the markup into IL at build time rather than parsing it at runtime, catching many typos and unknown-property errors as build errors instead of runtime crashes.

🏏

Cricket analogy: XAML separating UI from logic is like a scorecard (the visual layout) being kept separate from the team's tactical notes (the code-behind), where mislabeling a fielding position on the scorecard (an XAML typo) gets flagged before the match starts thanks to XamlC's build-time checks.

Layouts, Controls, and Data Binding

MAUI's core layout containers are VerticalStackLayout/HorizontalStackLayout (simple linear stacking with Spacing), Grid (row/column-based, defined via RowDefinitions/ColumnDefinitions and Grid.Row/Grid.Column attached properties), and FlexLayout (wrap-capable flexible box layout); controls like Label, Entry, Button, and CollectionView nest inside these layouts. Data binding connects a control's property to a C# object's property using the {Binding PropertyName} markup extension, most commonly against a BindingContext set to a ViewModel that implements INotifyPropertyChanged, so a change to the ViewModel's property automatically updates the bound UI without manual UI-refresh code — this is the foundation of the MVVM (Model-View-ViewModel) pattern MAUI apps typically follow.

🏏

Cricket analogy: Grid's RowDefinitions/ColumnDefinitions is like a scoreboard laid out in fixed rows for overs and columns for each batsman's runs, while {Binding} is like the scoreboard operator automatically updating a batsman's score the instant it changes on the official system.

xml
<!-- A Grid layout with data binding to a ViewModel -->
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="HelloMaui.MainPage"
             x:DataType="local:MainViewModel">

    <Grid RowDefinitions="Auto,*" ColumnDefinitions="*,Auto" Padding="20">
        <Label Text="{Binding Greeting}"
               Grid.Row="0" Grid.ColumnSpan="2"
               FontSize="22" />

        <Entry Text="{Binding UserName}"
               Grid.Row="1" Grid.Column="0"
               Placeholder="Enter your name" />

        <Button Text="Say Hi"
                Grid.Row="1" Grid.Column="1"
                Command="{Binding GreetCommand}" />
    </Grid>
</ContentPage>

XAML Markup Extensions and Resources

Beyond {Binding}, common markup extensions include {StaticResource key} to pull a value (a Color, a Style) from a ResourceDictionary defined in App.xaml or a page's Resources section, {x:Static} to reference a static C# field or property, and {AppThemeBinding Light=..., Dark=...} to swap a value automatically based on the system's light/dark theme. Styles defined once in App.xaml's ResourceDictionary (e.g., a Style targeting Button with a TargetType and Setters for BackgroundColor and CornerRadius) apply globally unless overridden locally, letting you keep visual consistency without repeating property values across every page's XAML.

🏏

Cricket analogy: {StaticResource} pulling a shared Color from App.xaml is like every team in a league pulling the official ball specification from the governing body's rulebook rather than each team defining its own, ensuring consistency across every match.

You can inspect and validate compiled bindings by adding x:DataType to a page or DataTemplate — this enables compile-time binding checks (catching a misspelled {Binding} property name as a build error) and measurably faster runtime binding performance compared to the default reflection-based binding.

Forgetting to set BindingContext (or x:DataType without a matching runtime BindingContext) means {Binding} expressions silently resolve to null at runtime instead of throwing — always verify BindingContext is assigned, either in code-behind (BindingContext = new MainViewModel();) or via constructor injection with a registered ViewModel.

  • XAML is a declarative XML syntax for defining a page's visual tree, paired with a C# code-behind file via a partial class and x:Class.
  • XamlC compiles XAML to IL at build time, catching many typos as build errors rather than runtime crashes.
  • Core layouts are VerticalStackLayout/HorizontalStackLayout, Grid (row/column-based), and FlexLayout (wrap-capable).
  • {Binding PropertyName} connects a control's property to a ViewModel property, typically via BindingContext and INotifyPropertyChanged — the basis of MVVM.
  • {StaticResource}, {x:Static}, and {AppThemeBinding} are markup extensions for resources, static references, and light/dark theming.
  • Styles defined once in App.xaml's ResourceDictionary apply globally and can be overridden locally per control.
  • x:DataType enables compile-time binding validation and better runtime binding performance versus default reflection-based binding.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#NETMAUIStudyNotes#XAMLBasics#XAML#Layouts#Controls#Data#StudyNotes#SkillVeris#ExamPrep