dotnet/maui · error · InvalidOperationException
Cannot convert null into {typeof(ButtonContentLayout)}
Error message
Cannot convert null into {typeof(ButtonContentLayout)} What it means
Thrown by ButtonContentLayoutTypeConverter.ConvertFrom when value?.ToString() returns null. The converter can only parse string representations of a ButtonContentLayout (e.g. the content layout XAML attribute); a null input has no string form and cannot be converted.
Source
Thrown at src/Controls/src/Core/Button/Button.cs:585
}
/// <summary>
/// A converter to convert a string to a <see cref="ButtonContentLayout"/> object.
/// </summary>
public sealed class ButtonContentTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> false;
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
// IMPORTANT! Update ButtonContentDesignTypeConverter.IsValid if making changes here
var strValue = value?.ToString();
if (strValue == null)
throw new InvalidOperationException($"Cannot convert null into {typeof(ButtonContentLayout)}");
string[] parts = strValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 1 && parts.Length != 2)
throw new InvalidOperationException($"Cannot convert \"{strValue}\" into {typeof(ButtonContentLayout)}");
double spacing = DefaultSpacing;
var position = ButtonContentLayout.ImagePosition.Left;
var spacingFirst = char.IsDigit(parts[0][0]);
int positionIndex = spacingFirst ? (parts.Length == 2 ? 1 : -1) : 0;
int spacingIndex = spacingFirst ? 0 : (parts.Length == 2 ? 1 : -1);
if (spacingIndex > -1)
spacing = double.Parse(parts[spacingIndex], CultureInfo.InvariantCulture);
if (positionIndex > -1)View on GitHub (pinned to f377ff1c5e)
Solutions
- Provide a non-null string value in the format expected (e.g. "Left,10" or "Top").
- Guard the source so it never evaluates to null before it reaches the converter.
- Use a fallback/default ButtonContentLayout instance instead of null.
Example fix
// before
button.ContentLayout = (ButtonContentLayout)
new ButtonContentTypeConverter().ConvertFrom(null, culture, null);
// after
button.ContentLayout = new ButtonContentLayout(ButtonContentLayout.ImagePosition.Left, 10); Defensive patterns
Strategy: validation
Validate before calling
if (value is null) return new ButtonContentLayout(ButtonContentLayout.ImagePosition.Left, ButtonContentLayout.DefaultSpacing); // otherwise proceed with converter
Type guard
static bool CanConvertToButtonContentLayout(object v) =>
v is string s && !string.IsNullOrWhiteSpace(s); Prevention
- Never pass null to the ContentLayout converter.
- Guard bindings so the source never evaluates to null.
- Use a default ButtonContentLayout instead of null.
When it happens
Trigger: Passing null to the converter. A XAML attribute resolving to null (binding evaluates to null, or a StaticResource missing). Programmatic TypeConverter.ConvertFrom(null) invocation.
Common situations: Binding the ContentLayout property to a source whose value becomes null. XAML with a missing/empty attribute routed through the converter. Serialization round-trips that lose the value.
Related errors
- Cannot convert "{strValue}" into {typeof(ButtonContentLayout
- Cannot convert "{0}" into {1}
- XC0040
- XC0040
- XC0040
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/f20aeda5333d4efa.
Report an issue: GitHub.