xaml-csharp-development-skill-for-avalonia

Media Colors, Brushes, and FormattedText Practical Usage

Table of Contents

  1. Scope and APIs
  2. Colors vs Brushes
  3. XAML Color/Brush Patterns
  4. FormattedText Construction and Metrics
  5. Range-Level Formatting APIs
  6. Drawing and Geometry Usage
  7. Best Practices
  8. Troubleshooting

Scope and APIs

Primary APIs:

Important FormattedText members:

Reference source files:

Colors vs Brushes

Use Colors when you need color math/conversion and Brushes when you need draw/fill/stroke instances.

using Avalonia.Media;

Color accent = Colors.CornflowerBlue;
IBrush textBrush = Brushes.White;

XAML Color/Brush Patterns

<Border xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Background="{DynamicResource AccentBrush}"
        BorderBrush="{StaticResource CardBorderBrush}"
        BorderThickness="1"
        Padding="12">
  <Border.Resources>
    <SolidColorBrush x:Key="AccentBrush" Color="CornflowerBlue" />
    <SolidColorBrush x:Key="CardBorderBrush" Color="#335A6A7A" />
  </Border.Resources>

  <TextBlock Foreground="White" Text="Brush resource usage" />
</Border>

Use resource keys for theme substitution instead of hardcoding many literal colors in controls.

FormattedText Construction and Metrics

using System.Globalization;
using Avalonia;
using Avalonia.Media;

FormattedText BuildTitle(string text)
{
    var ft = new FormattedText(
        text,
        CultureInfo.CurrentUICulture,
        FlowDirection.LeftToRight,
        new Typeface("Inter"),
        18,
        Brushes.White);

    ft.TextAlignment = TextAlignment.Left;
    ft.MaxTextWidth = 420;
    ft.Trimming = TextTrimming.CharacterEllipsis;

    return ft;
}

Use metrics (Width, Height, Baseline) to align custom drawing with surrounding primitives.

Range-Level Formatting APIs

FormattedText supports per-range updates without rebuilding the object.

using Avalonia.Media;

void EmphasizePrefix(FormattedText ft)
{
    ft.SetFontWeight(FontWeight.Bold, 0, 4);
    ft.SetForegroundBrush(Brushes.Gold, 0, 4);
    ft.SetTextDecorations(TextDecorations.Underline, 0, 4);
}

This is useful for compact custom-rendered labels where full text controls are unnecessary.

Drawing and Geometry Usage

In custom controls:

using System.Globalization;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;

public class BadgeControl : Control
{
    public override void Render(DrawingContext context)
    {
        base.Render(context);

        var ft = new FormattedText(
            "LIVE",
            CultureInfo.CurrentUICulture,
            FlowDirection.LeftToRight,
            new Typeface("Inter"),
            12,
            Brushes.White)
        {
            MaxTextWidth = Bounds.Width,
            TextAlignment = TextAlignment.Center
        };

        context.DrawRectangle(Brushes.Crimson, null, Bounds);
        context.DrawText(ft, new Point(0, (Bounds.Height - ft.Height) / 2));
    }
}

Geometry extraction pattern:

using Avalonia;
using Avalonia.Media;

Geometry? BuildTextOutline(FormattedText ft)
{
    return ft.BuildGeometry(new Point(0, 0));
}

BuildHighlightGeometry is useful for custom text-selection visualization.

Best Practices

Troubleshooting

  1. Text clips unexpectedly.
    • Check MaxTextWidth, MaxTextHeight, and MaxLineCount interactions.
  2. Trimming does not appear.
    • Ensure width/height constraints are actually limiting layout and Trimming is not None.
  3. Geometry from text is empty.
    • Confirm text length is non-zero and rendering metrics were initialized.
  4. Performance degrades in custom render loop.
    • Cache FormattedText and rebuild only when text/style inputs change.
  5. Color values appear correct but fill is wrong.
    • Verify brush assignment and alpha channel values (#AARRGGBB semantics).