AvaloniaUI/Avalonia · error · ArgumentException

Unknown CacheMode: {s}

Error message

Unknown CacheMode: {s}

What it means

CacheMode.Parse throws an ArgumentException because the parser currently recognizes only the literal string 'BitmapCache'. Any other input is an unknown cache mode. This is a deliberately narrow parser — the only supported cache mode value is BitmapCache.

Source

Thrown at src/Avalonia.Base/Media/CacheMode.cs:19

using System;
using Avalonia.Rendering.Composition;
using Avalonia.Rendering.Composition.Drawing;

namespace Avalonia.Media;

/// <summary>
/// Represents cached content modes for graphics acceleration features.
/// </summary>
public abstract class CacheMode : StyledElement
{
    // We currently only allow visual to be attached to one compositor at a time, so keep it simple for now
    internal abstract CompositionCacheMode GetForCompositor(Compositor c);

    public static CacheMode Parse(string s)
    {
        if(s == "BitmapCache")
            return new BitmapCache();
        throw new ArgumentException("Unknown CacheMode: " + s);
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use exactly "BitmapCache" as the string value.
  2. If you need other cache modes, construct the specific CacheMode subclass (e.g. new BitmapCache()) directly instead of parsing.
  3. Validate the string equals "BitmapCache" before calling Parse.

Example fix

// before
var mode = CacheMode.Parse("bitmapCache");

// after
var mode = CacheMode.Parse("BitmapCache");
// or directly:
var mode = new BitmapCache();
Defensive patterns

Strategy: validation

Validate before calling

CacheMode mode = string.Equals(s, "BitmapCache", StringComparison.Ordinal)
    ? new BitmapCache()
    : null; // or a fallback

Prevention

When it happens

Trigger: Calling CacheMode.Parse(s) where s is anything other than the exact string "BitmapCache" (case-sensitive). E.g. "bitmapcache", "BitmapCacheShading", or a typo.

Common situations: Typo in a XAML CacheMode attribute. Using lowercase or a variant spelling. Attempting to reference a cache mode that does not exist in this version of Avalonia.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/75d172f6436ab2be. Report an issue: GitHub.