dotnet/maui · error · InvalidOperationException
Cannot convert "{0}" into {1}
Error message
Cannot convert "{0}" into {1} What it means
The TextDecorationsConverter throws InvalidOperationException when ConvertFrom receives a value whose ToString() is null. Because the converter calls value?.ToString() and then checks for null, this is reached when the source object's ToString returns null — an edge case for null-valued input passed as a non-null object. The message formats the null string and the TextDecorations type.
Source
Thrown at src/Controls/src/Core/DecorableTextElement.cs:28
public static readonly BindableProperty TextDecorationsProperty = BindableProperty.Create(nameof(IDecorableTextElement.TextDecorations), typeof(TextDecorations), typeof(IDecorableTextElement), TextDecorations.None);
}
/// <summary>A <see cref="System.ComponentModel.TypeConverter"/> subclass that can convert between a string and a <see cref="Microsoft.Maui.TextDecorations"/> object.</summary>
public class TextDecorationConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
=> sourceType == typeof(string);
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
=> destinationType == typeof(string);
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
var strValue = value?.ToString();
TextDecorations result = TextDecorations.None;
if (strValue == null)
throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(TextDecorations)));
var valueArr = strValue.Split(',');
if (valueArr.Length <= 1)
valueArr = strValue.Split(' ');
foreach (var item in valueArr)
{
if (Enum.TryParse(item.Trim(), true, out TextDecorations textDecorations))
result |= textDecorations;
else if (item.Equals("line-through", StringComparison.OrdinalIgnoreCase))
result |= TextDecorations.Strikethrough;
else
throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", item, typeof(TextDecorations)));
}
return result;
}View on GitHub (pinned to f377ff1c5e)
Solutions
- Pass a string literal or comma/space-separated decoration names (e.g. 'underline', 'strikethrough', 'line-through').
- Ensure any custom source object's ToString() never returns null.
- Avoid routing null values into TextDecorations properties; use a fallback value.
Example fix
// before label.TextDecorations = (TextDecorations)converter.ConvertFrom(null, culture, objWithNullToString); // after label.TextDecorations = TextDecorations.None; // explicit default
Defensive patterns
Strategy: validation
Validate before calling
// Avoid passing values whose ToString() is null.
var s = value?.ToString();
if (s is null) throw new ArgumentException("value.ToString() returned null; cannot convert to TextDecorations."); Prevention
- Pass explicit decoration strings, never null-yielding objects.
- Ensure custom source types never return null from ToString().
- Default unset decorations to TextDecorations.None.
When it happens
Trigger: Passing an object whose ToString() implementation returns null (rare), or passing a null boxed value through a path that boxes it. The strValue==null guard is the only way to reach this throw.
Common situations: Custom types overriding ToString to return null; binding a null-valued non-string source to a TextDecorations property via the converter.
Related errors
- Cannot convert "{0}" into {1}
- Implement INativeElementView on cell renderer: {ContentCell.
- No UIViewController found to present.
- You MUST invoke LoadApplication () before calling base.Finis
- call Forms.Init() before this
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/df3052c930e715fe.
Report an issue: GitHub.