dotnet/maui · error · BuildException

XC0122

XC0122

Error message

Style property or Content is not a string literal.

What it means

Thrown when inline Style content is present but is not a string literal ValueNode (e.g. it is an element node or nested markup). The compiled provider can only emit a Ldstr + StyleSheet.FromString call, so the content must be a plain string of CSS.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledValueProviders/StyleSheetProvider.cs:33

		{
			((ElementNode)node).Properties.TryGetValue(new XmlName("", "Source"), out INode sourceNode);
			if (sourceNode == null)
				((ElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.MauiUri, "Source"), out sourceNode);

			INode styleNode = null;
			if (!((ElementNode)node).Properties.TryGetValue(new XmlName("", "Style"), out styleNode) &&
				!((ElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.MauiUri, "Style"), out styleNode) &&
				((ElementNode)node).CollectionItems.Count == 1)
				styleNode = ((ElementNode)node).CollectionItems[0];

			if (sourceNode != null && styleNode != null)
				throw new BuildException(BuildExceptionCode.StyleSheetSourceOrContent, node, null);

			if (sourceNode == null && styleNode == null)
				throw new BuildException(BuildExceptionCode.StyleSheetNoSourceOrContent, node, null);

			if (styleNode != null && styleNode is not ValueNode)
				throw new BuildException(BuildExceptionCode.StyleSheetStyleNotALiteral, node, null);

			if (sourceNode != null && sourceNode is not ValueNode)
				throw new BuildException(BuildExceptionCode.StyleSheetSourceNotALiteral, node, null);

			if (styleNode != null)
			{
				var style = (styleNode as ValueNode).Value as string;
				yield return Create(Ldstr, style);
				yield return Create(Call, module.ImportMethodReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls.StyleSheets", "StyleSheet"),
																	   methodName: "FromString",
																	   parameterTypes: [("mscorlib", "System", "String")],
																	   isStatic: true));
			}
			else
			{
				var source = (sourceNode as ValueNode)?.Value as string;
				INode rootNode = node;
				while (rootNode is not ILRootNode)

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Replace nested elements with raw CSS text inside <StyleSheet>.
  2. For platform-specific CSS, use separate <StyleSheet> elements per platform or handle selection in code-behind.
  3. If the CSS is large, move it to a .css resource file and use Source instead.

Example fix

// before
<StyleSheet>
  <OnPlatform x:TypeArguments="x:String" ... />
</StyleSheet>
// after
<StyleSheet>
  label { color: red; }
</StyleSheet>
Defensive patterns

Strategy: validation

Validate before calling

// Lint: inline StyleSheet content must be plain text
static bool StyleSheetContentIsLiteral(XElement el) =>
    el.Attribute("Source") == null && !el.Elements().Any() && !string.IsNullOrWhiteSpace(el.Value);

Prevention

When it happens

Trigger: Putting a child element inside <StyleSheet> instead of raw CSS text; binding the Style content; embedding a node like <StyleSheet><OnPlatform/></StyleSheet>.

Common situations: Trying to make the CSS platform-conditional via OnPlatform inside the style block; authoring structured nodes where a literal was expected; tooling that wraps text in an element.

Related errors


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