dotnet/maui · error · InvalidOperationException
Cannot convert "{strValue}" into {typeof(FlowDirection)}
Error message
Cannot convert "{strValue}" into {typeof(FlowDirection)} What it means
FlowDirectionConverter.ConvertFrom throws InvalidOperationException when the string is non-null but cannot be parsed as a FlowDirection enum value nor matched case-insensitively against the aliases 'ltr', 'rtl', 'inherit'. Anything else (typos, localized strings, numeric forms) is rejected.
Source
Thrown at src/Controls/src/Core/FlowDirectionConverter.cs:31
=> destinationType == typeof(string);
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
var strValue = value?.ToString();
if (strValue != null)
{
if (Enum.TryParse(strValue, out FlowDirection direction))
return direction;
if (strValue.Equals("ltr", StringComparison.OrdinalIgnoreCase))
return FlowDirection.LeftToRight;
if (strValue.Equals("rtl", StringComparison.OrdinalIgnoreCase))
return FlowDirection.RightToLeft;
if (strValue.Equals("inherit", StringComparison.OrdinalIgnoreCase))
return FlowDirection.MatchParent;
}
throw new InvalidOperationException($"Cannot convert \"{strValue}\" into {typeof(FlowDirection)}");
}
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
if (value is not FlowDirection direction)
throw new NotSupportedException();
return direction.ToString();
}
}
}View on GitHub (pinned to f377ff1c5e)
Solutions
- Use the exact enum names (LeftToRight, RightToLeft, MatchParent) or the supported aliases 'ltr', 'rtl', 'inherit'.
- Trim and validate the input string before assigning: `strValue?.Trim()`.
- Provide a fallback in the binding (ConverterParameter or FallbackValue) for unknown strings.
Example fix
<!-- before --> <Label FlowDirection="left" /> <!-- after --> <Label FlowDirection="LeftToRight" />
Defensive patterns
Strategy: validation
Validate before calling
var aliases = new[]{"ltr","rtl","inherit"}; var trimmed = strValue?.Trim(); if (Enum.TryParse<FlowDirection>(trimmed, out var fd) || (trimmed != null && aliases.Contains(trimmed.ToLower()))) { /* ok */ } Type guard
static bool IsValidFlowDirection(string s) => Enum.TryParse<FlowDirection>(s, out _) || s?.ToLower() is "ltr" or "rtl" or "inherit";
Try / catch
try { return new FlowDirectionConverter().ConvertFrom(value); } catch (InvalidOperationException) { return FlowDirection.MatchParent; } Prevention
- Use exact enum names or the documented aliases.
- Trim whitespace before assignment.
- Validate user-supplied strings against the allowed set.
When it happens
Trigger: Supplying a string like 'LeftToRight' (no alias match unless exact enum), 'left', 'right', 'auto', or a typo like 'ltr ' with trailing whitespace to a FlowDirection property through XAML or a converter.
Common situations: XAML authored with shorthand 'left'/'right', copy from CSS-direction semantics ('auto'), or trailing whitespace from data binding that bypasses enum parsing.
Related errors
- Cannot convert "{0}" into {1}
- throw new NotSupportedException();
- RootComponent requires a value for its Selector property, bu
- RootComponent requires a value for its ComponentType propert
- XC0040
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/badc2040a9678582.
Report an issue: GitHub.