dotnet/maui · error · BuildException

XC0040

XC0040

Error message

Cannot convert value "{0}" to "{1}".

What it means

XC0040 Conversion thrown by TypeTypeConverter when a XAML string representing a System.Type reference cannot be resolved. The converter splits on ':' to extract an optional XML namespace prefix and type name, resolves the namespace, then calls GetTypeReference. Failure occurs if the value is empty, contains more than one colon, the namespace prefix is undeclared, or the named type cannot be found in the resolved assembly.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/TypeTypeConverter.cs:44

			if (split.Length == 2)
				xmlType = new XmlType(node.NamespaceResolver.LookupNamespace(split[0]), split[1], null);
			else
				xmlType = new XmlType(node.NamespaceResolver.LookupNamespace(""), split[0], null);

			var typeRef = xmlType.GetTypeReference(context.Cache, module, (IXmlLineInfo)node);
			if (typeRef == null)
				goto error;

			yield return Create(Ldtoken, module.ImportReference(typeRef));
			yield return Create(Call, module.ImportMethodReference(context.Cache, ("mscorlib", "System", "Type"),
																   methodName: "GetTypeFromHandle",
																   parameterTypes: new[] { ("mscorlib", "System", "RuntimeTypeHandle") },
																   isStatic: true));

			yield break;

		error:
			throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(Type));
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Ensure the XML namespace prefix used in the type reference is declared at the root element (e.g. xmlns:local='clr-namespace:MyApp.Models').
  2. Verify the type name is spelled correctly and exists in the referenced namespace/assembly.
  3. Ensure the value contains at most one ':' separating the prefix from the type name.
  4. Confirm the assembly containing the type is referenced by the project.

Example fix

<!-- before (missing xmlns:vm declaration) -->
<ContentPage>
  <DataTemplate x:DataType="vm:ItemViewModel" />
</ContentPage>

<!-- after -->
<ContentPage xmlns:vm="clr-namespace:MyApp.ViewModels">
  <DataTemplate x:DataType="vm:ItemViewModel" />
</ContentPage>
Defensive patterns

Strategy: validation

Validate before calling

// Validate a Type XAML reference: non-empty, at most one prefix colon, namespace resolvable
static bool IsValidTypeReference(string value, Func<string, string> lookupNamespace)
{
    if (string.IsNullOrEmpty(value)) return false;
    var split = value.Split(':');
    if (split.Length > 2) return false;
    var prefix = split.Length == 2 ? split[0] : "";
    return lookupNamespace(prefix) != null;
}

Prevention

When it happens

Trigger: Setting an x:Type or Type-typed property (e.g. DataTemplate content, TargetType in a Style) to a type name with an undeclared namespace prefix, a non-existent type, or a malformed reference like 'local:MyType:Extra'. A missing or wrong xmlns declaration causes namespace lookup to fail.

Common situations: Forgetting to declare the xmlns:local (or other prefix) at the root element. Using a type name that does not exist or was renamed. Typo in the type name. The type is in an assembly that is not referenced.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/74aa4ddeb4e4f07a. Report an issue: GitHub.