unoplatform/uno · error · Exception

Unable to find x:Class on the top level element

Error message

Unable to find x:Class on the top level element

What it means

Thrown by EnsureXClassName() (a [MemberNotNull] guard) when the cached _xClassName field is null later in generation. Unlike the sibling errors this is a plain System.Exception, not an XamlGenerationException, so it lacks source location and produces a raw build error. It is a defensive re-check invoked from many sites (lines 281, 474, 1121, 1327, 3786, 4553, 6869) that need the root class identity. It indicates the earlier GetClassName path was bypassed or the root genuinely lacked x:Class.

Source

Thrown at src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs:2389

			var classMember = control.Members.FirstOrDefault(m => m.Member.Name == "Class");

			if (classMember?.Value != null)
			{
				var fullName = classMember.Value.ToString() ?? "";
				return metadataHelper.FindTypeByFullName(fullName) as INamedTypeSymbol;
			}
			else
			{
				return null;
			}
		}

		[MemberNotNull(nameof(_xClassName))]
		private void EnsureXClassName()
		{
			if (_xClassName == null)
			{
				throw new Exception("Unable to find x:Class on the top level element");
			}
		}

		private void BuildProperties(IIndentedStringBuilder writer, XamlObjectDefinition topLevelControl, bool isInline = true, string? closureName = null, bool useBase = false)
		{
			TryAnnotateWithGeneratorSource(writer);
			BuildSourceLineInfo(writer, topLevelControl);

			if (topLevelControl.Members.Count > 0)
			{
				var setterPrefix = string.IsNullOrWhiteSpace(closureName) ? string.Empty : closureName + ".";

				var implicitContentChild = FindImplicitContentMember(topLevelControl);

				if (implicitContentChild != null)
				{
					var topLevelControlSymbol = FindType(topLevelControl.Type);
					if (IsTextBlock(topLevelControlSymbol))

View on GitHub (pinned to 0418340488)

Solutions

  1. Treat this identically to error 301: ensure the root element has a valid non-empty x:Class attribute.
  2. Because this exception has no source line, bisect by temporarily disabling recently added XAML files to find which one lacks x:Class.
  3. If the file is a ResourceDictionary, make sure the root tag is <ResourceDictionary> so it takes the dictionary code path instead of the class path.
  4. Check that x:Class has a value (not x:Class="") and a namespace containing at least one dot.

Example fix

<!-- before (empty or missing class) -->
<UserControl xmlns:x="..." x:Class="">
  <Grid/>
</UserControl>

<!-- after -->
<UserControl x:Class="MyApp.Widgets.MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid/>
</UserControl>
Defensive patterns

Strategy: validation

Validate before calling

# Catch empty/missing x:Class (the value must be non-empty with a namespace dot)
Get-ChildItem -Recurse -Filter *.xaml | ForEach-Object {
  $m = Select-String -Path $_.FullName -Pattern 'x:Class="([^"]*)"'
  if ($m) {
    $val = $m.Matches[0].Groups[1].Value
    if ([string]::IsNullOrWhiteSpace($val) -or -not $val.Contains('.')) {
      Write-Warning "$($_.FullName): x:Class value '$val' is empty or has no namespace"
    }
  }
}

Prevention

When it happens

Trigger: The root element has no x:Class and execution reaches a code path that calls EnsureXClassName after the initial class resolution was skipped (e.g. a top-level dictionary path that then needs class context); the x:Class member exists but its Value is null so FindClassName returned null at line 2351-2366.

Common situations: Same root causes as error 301 but surfaced later; a XAML file that is neither a clean ResourceDictionary nor a class-backed control; an empty x:Class="" attribute; generated/injected XAML that omits the class member.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/0c2351ac7bab1117. Report an issue: GitHub.