dotnet/maui · error · BuildException

XC0062

XC0062

Error message

Undeclared xmlns prefix "{0}".

What it means

Thrown when parsing the x:DataType type expression raises a XamlParseException because the xmlns prefix used is not declared. The compiler extracts the prefix portion (before ':') from the DataType string and reports it as undeclared.

Source

Thrown at src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs:587

					&& enode.XmlType.Name == nameof(Xaml.NullExtension))
				{
					context.LoggingHelper.LogWarningOrError(BindingWithNullDataType, context.XamlFilePath, node.LineNumber, node.LinePosition, 0, 0, null);
					return false;
				}

				if ((dataTypeNode as ValueNode)?.Value is not string dataType)
					throw new BuildException(XDataTypeSyntax, dataTypeNode as IXmlLineInfo, null);

				XmlType dtXType = null;
				try
				{
					dtXType = TypeArgumentsParser.ParseSingle(dataType, node.NamespaceResolver, dataTypeNode as IXmlLineInfo)
						?? throw new BuildException(XDataTypeSyntax, dataTypeNode as IXmlLineInfo, null);
				}
				catch (XamlParseException)
				{
					var prefix = dataType.Contains(":") ? dataType.Substring(0, dataType.IndexOf(":", StringComparison.Ordinal)) : "";
					throw new BuildException(XmlnsUndeclared, dataTypeNode as IXmlLineInfo, null, prefix);
				}

				tSourceRef = dtXType.GetTypeReference(context.Cache, module, (IXmlLineInfo)node);
				if (tSourceRef == null)
					return false;
			}

			if (tSourceRef == null)
				return false; //throw

			if (!TryParsePath(context, path, tSourceRef, node as IXmlLineInfo, module, out var properties, suppressPropertyNotFoundError: tSourceRefIsAncestorTypeInferred))
			{
				return false;
			}

			instructions = GenerateInstructions();
			return true;

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Add the matching xmlns declaration, e.g. xmlns:vm="clr-namespace:App.ViewModels" (or an assembly-qualified url).
  2. Check the prefix spelling in both the xmlns declaration and the x:DataType value.
  3. Ensure the xmlns is declared on an ancestor in scope (typically the page/root element).

Example fix

<!-- before -->
<ContentPage x:DataType="vm:MainViewModel">

<!-- after -->
<ContentPage xmlns:vm="clr-namespace:App.ViewModels"
             x:DataType="vm:MainViewModel">
Defensive patterns

Strategy: validation

Validate before calling

// Ensure every prefix used in x:DataType is declared as an xmlns
static bool PrefixDeclared(string dataTypeValue, IEnumerable<string> declaredPrefixes)
{
    var prefix = dataTypeValue.Contains(':')
        ? dataTypeValue.Substring(0, dataTypeValue.IndexOf(':')) : null;
    return prefix == null || declaredPrefixes.Contains(prefix);
}

Prevention

When it happens

Trigger: x:DataType="prefix:Type" where `prefix` does not appear in any xmlns declaration on the element or its ancestors.

Common situations: Forgetting to add the xmlns declaration for the view-model namespace; typo in the prefix; prefix declared in a different scope than expected.

Related errors


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