dotnet/maui · error · BuildException

XC0121

XC0121

Error message

StyleSheet require either a Source or a content.

What it means

Thrown by the StyleSheet provider when neither a Source attribute nor inline Style/content is present. The provider needs at least one of them to construct a StyleSheet; with both null it has nothing to compile.

Source

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

	class StyleSheetProvider : ICompiledValueProvider
	{
		public IEnumerable<Instruction> ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context)
		{
			((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
			{

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Add a Source="path.css" attribute pointing to an embedded resource.
  2. Or add inline CSS as the element's text content / Style attribute.
  3. If unused, delete the <StyleSheet> element entirely.

Example fix

// before
<StyleSheet />
// after
<StyleSheet Source="Resources/Styles/app.css" />
Defensive patterns

Strategy: validation

Validate before calling

// Lint: a <StyleSheet> must have Source or content
static bool StyleSheetHasAnySource(XElement el) =>
    el.Attribute("Source") != null || !el.IsEmpty && !string.IsNullOrWhiteSpace(el.Value);

Prevention

When it happens

Trigger: An empty <StyleSheet /> element; a <StyleSheet> whose Style attribute and collection items are both absent; whitespace-only self-closing tag.

Common situations: Placeholder element left during scaffolding; copy-paste of a template that stripped the body; refactoring that moved CSS out but left the empty node.

Related errors


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