.NET MAUI

XAML & Markup Extensions

6 question(s)

What are markup extensions in XAML?

Beginner
Markup extensions provide values to XAML attributes at parse time using {} syntax, e.g., {Binding}, {StaticResource}, {DynamicResource}, {x:Static}, {OnPlatform}, {OnIdiom}. They let you reference resources, bind data, or compute values declaratively instead of hardcoding.
<Label Text="{Binding Title}"
       TextColor="{StaticResource Primary}"
       FontSize="{OnPlatform iOS=17, Android=16}" />
Real-world example One label pulls its text from a binding, its color from a resource, and a platform-specific font size—all via markup extensions.

Common follow-ups: Name a few built-in extensions. | What syntax do they use?

Markup XAML & UI Layouts XAML & Markup Extensions

What do OnPlatform and OnIdiom do?

Beginner
OnPlatform supplies different values per platform (iOS/Android/WinUI/MacCatalyst); OnIdiom supplies values per device type (Phone/Tablet/Desktop/TV/Watch). They're used inline in XAML to tweak sizes, margins, or resources without code-behind branching.
<StackLayout Padding="{OnPlatform iOS='20,40,20,20',
                                  Android='20'}"
             Spacing="{OnIdiom Phone=8, Desktop=16}" />
Real-world example Extra top padding accommodates the iOS status bar while Android uses uniform padding, and spacing grows on desktop.

Common follow-ups: OnPlatform vs OnIdiom? | Can they return complex values?

Markup Responsive UI XAML & Markup Extensions

What is x:Static and when do you use it?

Intermediate
x:Static references a static field, property, constant, or enum member from XAML, e.g., a color in a static class or an app constant. It's handy for sharing constants between C# and XAML without wrapping them in resources.
<Label TextColor="{x:Static local:AppColors.Brand}"
       Text="{x:Static local:AppStrings.Welcome}" />
Real-world example Brand colors and shared strings defined as C# constants are referenced directly in XAML via x:Static.

Common follow-ups: x:Static vs StaticResource? | Can it reference enums?

Markup Resources XAML & Markup Extensions

How do you create a custom markup extension?

Intermediate
Implement IMarkupExtension (or IMarkupExtension<T>) with a ProvideValue method returning the computed value, expose properties as inputs, and use it in XAML with {local:MyExtension ...}. Useful for translation lookups, computed values, or DI-resolved values.
public class TranslateExtension : IMarkupExtension<string>
{
    public string Key { get; set; }
    public string ProvideValue(IServiceProvider sp) =>
        AppResources.ResourceManager.GetString(Key) ?? Key;
    object IMarkupExtension.ProvideValue(IServiceProvider sp)
        => ProvideValue(sp);
}
Real-world example A {local:Translate Key=Welcome} extension localizes text inline from resource files.

Common follow-ups: What method must you implement? | Give a use case.

Markup Localization XAML & Markup Extensions

What is the difference between StaticResource and DynamicResource?

Advanced
StaticResource resolves the resource once at load time—efficient but won't reflect later changes. DynamicResource keeps a live link, updating the target if the resource is replaced (e.g., swapping a theme dictionary at runtime). Use DynamicResource only where runtime changes are needed, since it has slight overhead.
<!-- Updates live if 'Primary' is swapped at runtime -->
<Button BackgroundColor="{DynamicResource Primary}" />
Real-world example Theme colors use DynamicResource so switching light/dark at runtime recolors the UI instantly, while static values use StaticResource.

Common follow-ups: When must you use DynamicResource? | What's the performance trade-off?

Resources Themes XAML & Markup Extensions

How do compiled XAML (XamlC) and x:DataType improve XAML?

Advanced
XAML compilation (on by default) converts XAML to IL at build time, catching markup errors early and speeding load versus runtime parsing. Adding x:DataType enables compiled bindings, replacing reflection with generated accessors. Together they give build-time validation and better runtime performance.
[XamlCompilation(XamlCompilationOptions.Compile)]  // default in MAUI
// plus x:DataType on views for compiled bindings
Real-world example A typo in a XAML element name becomes a build error instead of a runtime crash because XAML is compiled.

Common follow-ups: What does x:DataType add? | Is XAML compilation on by default?

Performance & Optimization Data Binding & MVVM XAML & Markup Extensions