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
