unoplatform/uno · error · XamlObjectWriterException
Could not convert object
Error message
Could not convert object '{0}' (of type {1}) to {2}: What it means
A wrapper thrown by GetCorrectlyTypedValue when coercing a value to the target type/member throws any exception that is not already a XamlObjectWriterException. The inner exception holds the real conversion error, and its message is appended after the colon. The text 'Could not convert object ...' is the generic framing.
Solutions
- Read the InnerException message (appended after the colon) to find the real conversion failure.
- Correct the source value so the type converter accepts it.
- Verify the expected TypeConverter is registered for the target type and is functioning.
Example fix
<!-- before --> <Slider Value="abc" /> <!-- after --> <Slider Value="42" />
Defensive patterns
Strategy: try-catch
Try / catch
try { /* load/write XAML */ }
catch (XamlObjectWriterException ex) when (ex.Message.Contains("Could not convert")) {
// ex.InnerException has the converter's failure; fix the source value or the TypeConverter.
throw new InvalidOperationException("Type conversion failed", ex.InnerException);
} Prevention
- Validate string values match the target type's converter before/while authoring XAML.
- Unit-test custom TypeConverter.ConvertFrom with representative inputs.
- Always read InnerException to find the real conversion error.
When it happens
Trigger: A registered TypeConverter throws during ConvertFrom — e.g. Int32Converter receiving 'abc', a custom converter rejecting input, or a culture-sensitive parse failing.
Common situations: Malformed string values in XAML attributes; custom TypeConverter bugs; locale/culture parsing differences; null where a value type was expected.
Related errors
- An error occurred on getting provided value
- Value ' ' (of type ) is not of or convertible to type…
- Event has no underlying member to attach event
- Member ' ' is already written to current type
- Referenced type in event has no underlying type
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/91d8be833c3c9c37.
Report an issue: GitHub.
Appendix: source
Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectWriter.cs:500
if (xt.IsDictionary)
mt.Invoker.AddToDictionary (parent, GetCorrectlyTypedValue (null, xt.KeyType, keyObj), GetCorrectlyTypedValue (null, xt.ItemType, obj));
else // collection. Note that state.Type isn't usable for PositionalParameters to identify collection kind.
mt.Invoker.AddToCollection (parent, GetCorrectlyTypedValue (null, xt.ItemType, obj));
return true;
}
else
return false;
}
object GetCorrectlyTypedValue (XamlMember xm, XamlType xt, object value)
{
try {
return DoGetCorrectlyTypedValue (xm, xt, value);
} catch (XamlObjectWriterException) {
throw;
} catch (Exception ex) {
// For + ex.Message, the runtime should print InnerException message like .NET does.
throw new XamlObjectWriterException (String.Format (CultureInfo.InvariantCulture, "Could not convert object \'{0}' (of type {1}) to {2}: ", value, value != null ? (object) value.GetType () : "(null)", xt) + ex.Message, ex);
}
}
// It expects that it is not invoked when there is no value to
// assign.
// When it is passed null, then it returns a default instance.
// For example, passing null as Int32 results in 0.
// But do not immediately try to instantiate with the type, since the type might be abstract.
object DoGetCorrectlyTypedValue (XamlMember xm, XamlType xt, object value)
{
if (value == null) {
if (xt.IsContentValue (service_provider)) // it is for collection/dictionary key and item
return null;
else
return xt.IsNullable ? null : xt.Invoker.CreateInstance (Array.Empty<object>());
}
if (xt == null)
return value;View on GitHub (pinned to 0418340488)