unoplatform/uno · error · XamlObjectWriterException

Value '{0}' (of type {1}) is not of or convertible to type {

Error message

Value '{0}' (of type {1}) is not of or convertible to type {0} (member {3})

What it means

Thrown when a value is not assignable to the target XamlType and no TypeConverter could convert it — the final fallback after IsAllowedType and converter attempts both fail. Note: the format string has a placeholder bug ('type {0}' reuses index 0 instead of index 2 for the target type), so the rendered message may print the value where the target type should appear.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectWriter.cs:546

			// FIXME: this could be generalized by some means, but I cannot find any.
			if (xt.UnderlyingType == typeof (Type))
				value = new TypeExtension ((string) value).ProvideValue (service_provider);
			if (xt == XamlLanguage.Type && value is string)
				value = new TypeExtension ((string) value);
			
			if (IsAllowedType (xt, value))
				return value;

			var xtc = (xm != null ? xm.TypeConverter : null) ?? xt.TypeConverter;
			if (xtc != null && value != null) {
				var tc = xtc.ConverterInstance;
				if (tc != null && tc.CanConvertFrom (value.GetType ()))
					value = tc.ConvertFrom (value);
				return value;
			}

			throw new XamlObjectWriterException (String.Format (CultureInfo.InvariantCulture, "Value '{0}' (of type {1}) is not of or convertible to type {0} (member {3})", value, value != null ? (object) value.GetType () : "(null)", xt, xm));
		}

		XamlType ResolveTypeFromName (string name)
		{
			var nsr = (IXamlNamespaceResolver) service_provider.GetService (typeof (IXamlNamespaceResolver));
			return sctx.GetXamlType (XamlTypeName.Parse (name, nsr));
		}

		bool IsAllowedType (XamlType xt, object value)
		{
			return  xt == null ||
				xt.UnderlyingType == null ||
				xt.UnderlyingType.IsInstanceOfType (value) ||
				value == null && xt == XamlLanguage.Null ||
				xt.IsMarkupExtension && IsAllowedType (xt.MarkupExtensionReturnType, value);
		}
		
		void InitializeObjectIfRequired (bool waitForParameters)

View on GitHub (pinned to 0418340488)

Solutions

  1. Assign a value of the member's expected type (or a derived type).
  2. Add a [TypeConverter] on the target type so string/object conversion succeeds.
  3. Verify the member's XamlType and its TypeConverter are correctly resolved by the schema context.

Example fix

<!-- before -->
<Rectangle Fill="Red" Tag="notInt" />
<!-- after: ensure value matches member type -->
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the value is assignable to the member type or a converter exists.
if (!IsAllowedType(memberType, value)) {
    var tc = memberType.TypeConverter?.ConverterInstance;
    if (tc == null || !tc.CanConvertFrom(value.GetType()))
        throw new InvalidOperationException($"Value type {value.GetType()} not assignable to {memberType}.");
}

Try / catch

try { /* load/write XAML */ }
catch (XamlObjectWriterException ex) when (ex.Message.Contains("not of or convertible")) {
    // assign a compatible value or add a TypeConverter to the target type.
}

Prevention

When it happens

Trigger: Assigning a value whose type is incompatible with the member's type with no conversion path — e.g. a SolidColorBrush into an int property, or a string into a struct with no TypeConverter.

Common situations: Wrong object type placed in a XAML collection/property; TypeConverter attribute missing on a value type; XamlType with null UnderlyingType receiving concrete objects.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/3662002eaa7ffa1f. Report an issue: GitHub.