unoplatform/uno · error · InvalidOperationException
Invalid value '{part}', unable to parse.
Error message
Invalid value '{part}', unable to parse. What it means
Thrown by the StarStackPanel test-helper control when parsing a comma-separated list of GridLength segments (e.g. a ColumnDefinitions/RowDefinitions string). Each non-empty segment is matched against GridLengthParsingRegex, which accepts forms like 'Auto', '*', '2*', or a fixed pixel value. If a segment is neither empty nor matched by the regex, the parser cannot produce a GridLength and aborts. This is sample/test infrastructure, not shipping framework API.
Source
Thrown at src/SamplesApp/SamplesApp.UnitTests.Shared/Controls/UITests/Views/Controls/StarStackPanel.cs:552
private static GridLength[] ParseGridLength(string s)
{
var parts = s.Split(new[] { ',' });
var result = new List<GridLength>(parts.Length);
foreach (var part in parts)
{
if (string.IsNullOrEmpty(part))
{
result.Add(GridLengthHelper2.FromValueAndType(0, GridUnitType.Auto));
continue;
}
var match = GridLengthParsingRegex.Match(part);
if (!match.Success)
{
throw new InvalidOperationException("Invalid value '" + part + "', unable to parse.");
}
var autoGroup = match.Groups["auto"];
if (autoGroup.Success)
{
result.Add(GridLengthHelper2.FromValueAndType(0, GridUnitType.Auto));
continue;
}
var starsGroup = match.Groups["stars"];
if (starsGroup.Success)
{
var value =
!string.IsNullOrWhiteSpace(starsGroup.Value)
? double.Parse(starsGroup.Value, CultureInfo.InvariantCulture)
: 1;
result.Add(GridLengthHelper2.FromValueAndType(value, GridUnitType.Star));
continue;View on GitHub (pinned to 0418340488)
Solutions
- Inspect the exact '{part}' value in the message and correct it to a valid GridLength form: 'Auto', '*', 'N*' (e.g. '2*'), or a plain number for pixels.
- Sanitize the input string before parsing: trim whitespace and split on a culture-invariant separator so decimal values are not mis-split.
- If extending StarStackPanel, broaden GridLengthParsingRegex to accept the additional units you need, or pre-validate each segment and report all bad parts at once.
- Reproduce with a unit test feeding the failing string to isolate which token breaks the regex.
Example fix
// before
StarStackPanel.SetValues("1*, 100px, Auto"); // '100px' is not a valid GridLength
// after
StarStackPanel.SetValues("1*, 100, Auto"); Defensive patterns
Strategy: validation
Validate before calling
static readonly Regex _valid = new Regex(@"^\s*(auto|\d*\.?\d*\*?|\d+(\.\d+)?)\s*$", RegexOptions.IgnoreCase);
static IEnumerable<string> FindBadParts(string input) => input.Split(',').Where(p => !string.IsNullOrWhiteSpace(p) && !_valid.IsMatch(p.Trim())); Type guard
static bool IsValidGridLengthString(string s) => string.IsNullOrWhiteSpace(s) || s.Split(',').All(p => p.Trim() == "" || _valid.IsMatch(p.Trim())); Try / catch
try { panel.SetValues(text); } catch (InvalidOperationException ex) when (ex.Message.Contains("unable to parse")) { log.Warn($"Bad GridLength input '{text}': {ex.Message}"); panel.SetValues(SafeDefault); } Prevention
- Validate the definitions string with a regex before passing it to StarStackPanel.
- Use CultureInfo.InvariantCulture when splitting on decimal separators.
- Add a unit test that round-trips canonical GridLength strings through the parser.
When it happens
Trigger: Calling the parsing path of StarStackPanel (or a test/sample driving it) with a string that contains a token the regex does not recognize, such as '1*, foo, Auto' or a value with stray characters/units like '100px'.
Common situations: Typos in a manually authored definitions string in a sample page, copy-pasting CSS-style units ('px','em') into a GridLength list, locale-specific decimal separators ('1,5*' under comma cultures being split early), or trailing/leading whitespace combined with an invalid token.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- 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
- Failed to load animation.
- Vertex shader failed to compile: {infoLog}
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/58231b1059d1dfb6.
Report an issue: GitHub.