HandyOrg/HandyControl · error · FormatException
InvalidStringColLayout
Error message
InvalidStringColLayout
What it means
ColLayout.Parse throws FormatException('InvalidStringColLayout') when the input string is null, empty or whitespace, because no column lengths can be extracted from it. Parse is the inverse of ColLayout.ToString and is meant for strings like '24,12,8,4,2,1'.
Solutions
- Ensure the string is non-empty before calling ColLayout.Parse
- Provide a default layout when the source string is blank, e.g. string.IsNullOrEmpty(s) ? ColLayout.Default : ColLayout.Parse(s)
- Catch FormatException around Parse and fall back to a default layout
Example fix
// before var layout = ColLayout.Parse(settings.ColLayout); // throws when empty // after var layout = string.IsNullOrWhiteSpace(settings.ColLayout) ? ColLayout.Default : ColLayout.Parse(settings.ColLayout);
Defensive patterns
Strategy: validation
Validate before calling
if (!string.IsNullOrWhiteSpace(s)) { var layout = ColLayout.Parse(s); } Type guard
static bool IsValidColLayoutString(string? s) => !string.IsNullOrWhiteSpace(s);
Try / catch
try { return ColLayout.Parse(s); }
catch (FormatException) { return ColLayout.Default; } Prevention
- Validate input strings before parsing
- Default empty config values to ColLayout.Default
- Use ColLayout objects in code instead of round-tripping strings
When it happens
Trigger: Passing null, "", or whitespace-only strings to ColLayout.Parse, typically from XAML attributes or config values that were never set.
Common situations: Binding a grid definition from app settings or a database field that is empty; XAML where the ColLayout attribute was left blank; deserialization of incomplete config.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14).
Data as JSON: /api/errors/92f67a6eec02db57.
Report an issue: GitHub.
Appendix: source
Thrown at src/Avalonia/HandyControl_Avalonia/Tools/Extension/ColLayout.cs:104
}
return ColLayoutStatus.Sm;
}
return ColLayoutStatus.Md;
}
if (width < XlMaxWidth)
{
if (width < LgMaxWidth)
{
return ColLayoutStatus.Lg;
}
return ColLayoutStatus.Xl;
}
return ColLayoutStatus.Xxl;
}
public static ColLayout Parse(string s)
{
if (string.IsNullOrWhiteSpace(s)) throw new FormatException("InvalidStringColLayout");
var separators = new[] { ',', ' ' };
var tokens = s.Split(separators, StringSplitOptions.RemoveEmptyEntries);
var lengths = new int[6];
var i = 0;
foreach (var token in tokens)
{
if (i >= 6) throw new FormatException("InvalidStringColLayout");
lengths[i++] = int.Parse(token, CultureInfo.InvariantCulture);
}
return i switch
{
1 => new ColLayout(lengths[0]),
2 => new ColLayout { Xs = lengths[0], Sm = lengths[1] },
3 => new ColLayout { Xs = lengths[0], Sm = lengths[1], Md = lengths[2] },
4 => new ColLayout { Xs = lengths[0], Sm = lengths[1], Md = lengths[2], Lg = lengths[3] },
5 => new ColLayoutView on GitHub (pinned to 2c0875ebd6)