liveBook · Manning

ResourceDictionary

As explained in .NET MAUI in Action

ResourceDictionary is a XAML dictionary that maps reusable resources, such as styles and templates, to keys.

A ResourceDictionary stores reusable XAML resources under keys. Define a value once, then retrieve it where a control needs it, keeping repeated visual settings out of individual controls. For example, a calculator screen color can be named in Styles.xaml and applied to a Label with StaticResource. section 5.1.2

Define and use a named color

Place the color in a ResourceDictionary, give it an x:Key, then reference that key from a property: <ResourceDictionary ...> <Color x:Key="LcdBackgroundColor">#D1E0BA</Color> </ResourceDictionary> <Label BackgroundColor="{StaticResource LcdBackgroundColor}" /> This lets the LCD Label use the chosen background color without embedding its value directly in the page. section 5.1.2

Resource dictionaries can also reach settings that handler code cannot change. On Windows, the focus underline for a TextBox is controlled through resources in the platform App.xaml file, so keyed Thickness values set the normal and focused border thickness to zero. section 11.3.3

Listing 11.18 The code to add to App.xaml in the Windows platform folder
<maui:MauiWinUIApplication.Resources>
    <Thickness x:Key="TextControlBorderThemeThickness">0</Thickness>
    <Thickness x:Key="TextControlBorderThemeThicknessFocused">0</Thickness>
</maui:MauiWinUIApplication.Resources>

This App.xaml resource block shows two Windows theme resources overridden with zero Thickness values to remove the TextBox border and focused underline. section 11.3.3

When changing a resource, consider its scope and side effects:

  • Use a named resource such as LcdBackgroundColor when several properties or views should refer to one chosen value. section 5.1.2
  • Check controls that use the affected native control underneath. A Windows TextBox resource override also affects SearchBar because it uses TextBox internally. section 11.3.3
  • If a broad platform override is too wide, consider a platform-specific subclass and mapping, or add a targeted visual workaround such as a Border around the affected SearchBar. section 11.3.3

Use a ResourceDictionary for shared named values and platform resource overrides; keep scope in mind before replacing resources used by a base native control. section 11.3.3 section 5.1.2

How .NET MAUI in Action covers it

How deep it goes
Demonstrates defining and consuming a keyed Color, then overriding two Windows Thickness resources; defers fuller ResourceDictionary detail.
Examples
Uses an LCD background color in MauiCalc and zero Thickness resources for Windows TextBox border and focus styling.
What it assumes
Introduces ResourceDictionary use through a concrete XAML color example without assuming prior MAUI resource knowledge.
What you can do after
Apply it via XAML resources to reuse a named color and override supported Windows theme values.
How it connects
Connects ResourceDictionary with XAML, StaticResource, App.xaml, Styles.xaml, TextBox, SearchBar, and handler mappings.

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.