dotnet/maui · error · BuildException

XC0102

XC0102

Error message

x:DataType expects a string literal, an {{x:Type}} markup or {{x:Null}}.

What it means

Thrown by the compiled-binding logic when the x:DataType node's value is not a string literal. x:DataType must be a plain type-name string, an {x:Type ...} markup, or {x:Null}; an element/markup node that is none of these is rejected before parsing.

Source

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

					return false;
				}

				if (xDataTypeIsInOuterScope)
				{
					context.LoggingHelper.LogWarningOrError(BindingWithXDataTypeFromOuterScope, context.XamlFilePath, node.LineNumber, node.LinePosition, 0, 0, null);
					// continue compilation
				}

				if (   dataTypeNode is ElementNode enode
					&& enode.XmlType.NamespaceUri == XamlParser.X2009Uri
					&& 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;
			}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Set x:DataType to a string literal type name, e.g. `x:DataType="vm:MainViewModel"`.
  2. Use {x:Null} if you intentionally want a null data type (note: this emits a warning instead).
  3. Use the {x:Type ...} markup form if you need a Type reference.

Example fix

<!-- before -->
<ContentPage x:DataType="{StaticResource VmType}">

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

Strategy: validation

Validate before calling

// x:DataType must be a literal type string, {x:Type}, or {x:Null}
static bool IsValidXDataType(string raw)
    => !string.IsNullOrWhiteSpace(raw)
       && (raw == "{x:Null}" || raw.StartsWith("{x:Type") || !raw.StartsWith("{"));

Prevention

When it happens

Trigger: x:DataType is set to a non-literal such as a StaticResource/DynamicResource binding or a nested element: `x:DataType="{StaticResource T}"`.

Common situations: Developer tries to data-bind the DataType itself, or pastes a markup extension where a literal type name is required.

Related errors


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