liveBook · Manning

Explicit style

As explained in .NET MAUI in Action

An explicit style is a .NET MAUI style identified by a key and applied selectively to elements.

An explicit style is a .NET MAUI style identified by an x:Key and applied selectively through a control’s Style property. It groups Setters for a target control type, letting specific views share coordinated property values without changing every instance. section 10.1.1

Use an explicit style when:

  • Only selected instances of a control should receive the visual customization. section 10.4
  • Several properties should be kept together, such as font size, alignment, and text color. section 10.1.1
  • A specific style must take precedence over app-wide, page-specific, or control-level styles. section 10.4

Styling a count label

Define a keyed style for Label controls: <Style TargetType="Label" x:Key="CountLabelStyle"><Setter Property="FontSize" Value="64"/><Setter Property="HorizontalOptions" Value="Center"/><Setter Property="VerticalOptions" Value="Center"/><Setter Property="TextColor" Value="{StaticResource Primary}"/></Style>. Apply it only where needed with Style="{StaticResource CountLabelStyle}" on the count Label. section 10.1.1

Listing 10.3 The CountLabelStyle style
<Style TargetType="Label" x:Key="CountLabelStyle">
    <Setter Property="FontSize" Value="64"/>
    <Setter Property="HorizontalOptions" Value="Center"/>
    <Setter Property="VerticalOptions" Value="Center"/>
    <Setter Property="TextColor" Value="{StaticResource Primary}"/>
</Style>

Inspect the complete CountLabelStyle definition, including its target type, key, and property setters. section 10.1.1

<Label Grid.Row="2"
    Text="{Binding Count}"
    Style="{StaticResource CountLabelStyle}"/>

Inspect the XAML usage that applies CountLabelStyle to one Label through StaticResource. section 10.1.1

How .NET MAUI in Action covers it

How deep it goes
Defines target types, keyed resources, Setters, and precedence, but does not detail runtime style mutation or visual-state implementation.
Examples
Uses a keyed CountLabelStyle to format a bound count Label with size, alignment, and color setters.
What it assumes
Assumes basic .NET MAUI XAML controls and properties while defining TargetType, Setters, keys, and resource references.
What you can do after
Apply it via XAML resources and StaticResource references to reuse styling on chosen controls.
How it connects
Connects explicit styles with implicit styles, Setters, StaticResource, resource dictionaries, and style precedence.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET MAUI in Action. The section links point to the parts of the book it draws on.