dotnet/maui · error · BuildException

XC0120

XC0120

Error message

StyleSheet cannot have both a Source and a content.

What it means

Thrown by the StyleSheet value provider when a <StyleSheet> element declares both a Source attribute and inline Style content (or a single collection item). A stylesheet must come from exactly one place: an external resource file (Source) or an inline CSS literal (Style/content), not both.

Source

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

namespace Microsoft.Maui.Controls.XamlC
{
	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));

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Remove either the Source attribute or the inline Style/content, keeping exactly one.
  2. If you need both sources, split into two separate <StyleSheet> elements.
  3. After editing, rebuild to confirm only one of sourceNode/styleNode is non-null.

Example fix

// before
<StyleSheet Source="shared.css">
  label { color: red; }
</StyleSheet>
// after
<StyleSheet Source="shared.css" />
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Writing <StyleSheet Source="styles.css"> label { color: red; } </StyleSheet>; declaring both Source="..." and a Style attribute; having one collection item plus a Source.

Common situations: Migrating from inline to external CSS and forgetting to remove the inline block; merging two XAML fragments; IDE auto-completing the Source while inline content remains.

Related errors


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