dotnet/wpf · error · FormatException

SR.Parsers_IllegalToken

Error message

SR.Parsers_IllegalToken

What it means

CacheMode.FromParseString only recognizes the literal string "BitmapCache". Any other token (including "null" or misspellings) throws FormatException with SR.Parsers_IllegalToken because no other CacheMode has a parser token.

Solutions

  1. Use exactly the string "BitmapCache" (case-sensitive) or omit the attribute for the default.
  2. Remove the CacheMode attribute entirely if caching is not desired.
  3. If the value comes from user input, validate/normalize it before parsing.

Example fix

// before
<Rectangle CacheMode="bitmapcache" ... />

// after
<Rectangle CacheMode="BitmapCache" ... />
Defensive patterns

Strategy: try-catch

Validate before calling

bool isValid = value == null || string.Equals(value, "BitmapCache", StringComparison.Ordinal);

Type guard

static bool IsBitmapCacheToken(string s) => string.Equals(s, "BitmapCache", StringComparison.Ordinal);

Try / catch

try { cacheMode = (CacheMode)parser(value); }
catch (FormatException) { cacheMode = null; /* or default to BitmapCache */ }

Prevention

When it happens

Trigger: Parsing XAML or string values for a CacheMode-typed property with a value other than exactly "BitmapCache" (case-sensitive).

Common situations: Typing CacheMode="bitmapcache" (wrong case) or "NoCache" in XAML; binding a string from config/settings into a CacheMode converter.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7d43bfcfc39393fb. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CacheMode.cs:29

        
        /// <summary>
        /// Parse - this method is called by the type converter to parse a CacheMode's string 
        /// (provided in "value").
        /// </summary>
        /// <returns>
        /// A CacheMode which was created by parsing the "value" argument.
        /// </returns>
        /// <param name="value"> String representation of a CacheMode. Cannot be null/empty. </param>
        internal static CacheMode Parse(string value)
        {
            CacheMode cacheMode = null;
            if (value == "BitmapCache")
            {
                cacheMode = new BitmapCache();
            }
            else
            {
                throw new FormatException(SR.Parsers_IllegalToken);
            }
    
            return cacheMode;
        }

        /// <summary>
        /// Can serialze "this" to a string
        /// </summary>
        internal virtual bool CanSerializeToString()
        {
            return false;
        }

        internal virtual string ConvertToString(string format, IFormatProvider provider)
        {
            return base.ToString();
        }
    }

View on GitHub (pinned to 81131a70a4)