unoplatform/uno · error · FormatException
Color code must start with #
Error message
Color code must start with #
What it means
ParseColorCode parses XAML hex color literals and requires the value to begin with '#'. Any input lacking the leading '#' throws FormatException. This runs during XAML code generation whenever a color attribute/resource resolves to a hex string.
Source
Thrown at src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/Utils/ColorCodeParser.cs:18
#nullable enable
using System;
namespace Uno.UI.SourceGenerators.XamlGenerator.Utils
{
internal static class ColorCodeParser
{
public static string ParseColorCode(string colorCode)
{
if (colorCode == null)
{
throw new ArgumentNullException(nameof(colorCode));
}
if (!colorCode.StartsWith("#", StringComparison.Ordinal))
{
throw new FormatException("Color code must start with #");
}
byte a;
byte r;
byte g;
byte b;
if (colorCode.Length == 4)
{
a = 0xFF;
r = Convert.ToByte(new string(colorCode[1], 2), 16);
g = Convert.ToByte(new string(colorCode[2], 2), 16);
b = Convert.ToByte(new string(colorCode[3], 2), 16);
}
else if (colorCode.Length == 5)
{
a = Convert.ToByte(new string(colorCode[1], 2), 16);
r = Convert.ToByte(new string(colorCode[2], 2), 16);View on GitHub (pinned to 0418340488)
Solutions
- Prefix the color value with '#', e.g. Color="#FFFF0000".
- Use a XAML-recognized named color or a {ThemeResource}/{StaticResource} brush instead of a raw hex string where appropriate.
- Validate color literals before they reach the generator.
Example fix
<!-- before --> <SolidColorBrush Color="FF0000" /> <!-- after --> <SolidColorBrush Color="#FFFF0000" />
Defensive patterns
Strategy: validation
Validate before calling
static bool IsValidHexColor(string s) => !string.IsNullOrEmpty(s) && s.StartsWith("#", StringComparison.Ordinal);
// reject color literals before they reach the generator if !IsValidHexColor(value) Prevention
- Always prefix XAML hex colors with '#'.
- Use named colors or {ThemeResource}/{StaticResource} brushes for theme colors.
When it happens
Trigger: A color value passed to the parser does not start with '#', e.g. a bare hex 'FF0000', a named color like 'Red' routed where hex is expected, or an empty/garbage string.
Common situations: Hand-edited XAML with a Color attribute missing the '#'; a resource/binding that evaluates to a non-hex string at generation time.
Related errors
- Failed to parse color code: #{colorCode}
- The {0} string passed in the colorString argument is not a r
- The {0} string passed in the colorString argument is not a r
- The {0} string passed in the colorString argument is not a r
- Expected end of x:Bind expression or start of argument list.
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/d7afb0c51b8ea045.
Report an issue: GitHub.