{"record":{"id":"0c2351ac7bab1117","repo":"unoplatform/uno","slug":"unable-to-find-x-class-on-the-top-level-element","errorCode":null,"errorMessage":"Unable to find x:Class on the top level element","messagePattern":"Unable to find x:Class on the top level element","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs","lineNumber":2389,"sourceCode":"\t\t\tvar classMember = control.Members.FirstOrDefault(m => m.Member.Name == \"Class\");\n\n\t\t\tif (classMember?.Value != null)\n\t\t\t{\n\t\t\t\tvar fullName = classMember.Value.ToString() ?? \"\";\n\t\t\t\treturn metadataHelper.FindTypeByFullName(fullName) as INamedTypeSymbol;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\n\t\t[MemberNotNull(nameof(_xClassName))]\n\t\tprivate void EnsureXClassName()\n\t\t{\n\t\t\tif (_xClassName == null)\n\t\t\t{\n\t\t\t\tthrow new Exception(\"Unable to find x:Class on the top level element\");\n\t\t\t}\n\t\t}\n\n\t\tprivate void BuildProperties(IIndentedStringBuilder writer, XamlObjectDefinition topLevelControl, bool isInline = true, string? closureName = null, bool useBase = false)\n\t\t{\n\t\t\tTryAnnotateWithGeneratorSource(writer);\n\t\t\tBuildSourceLineInfo(writer, topLevelControl);\n\n\t\t\tif (topLevelControl.Members.Count > 0)\n\t\t\t{\n\t\t\t\tvar setterPrefix = string.IsNullOrWhiteSpace(closureName) ? string.Empty : closureName + \".\";\n\n\t\t\t\tvar implicitContentChild = FindImplicitContentMember(topLevelControl);\n\n\t\t\t\tif (implicitContentChild != null)\n\t\t\t\t{\n\t\t\t\t\tvar topLevelControlSymbol = FindType(topLevelControl.Type);\n\t\t\t\t\tif (IsTextBlock(topLevelControlSymbol))","sourceCodeStart":2371,"sourceCodeEnd":2407,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs#L2371-L2407","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Treat this identically to error 301: ensure the root element has a valid non-empty x:Class attribute.","Because this exception has no source line, bisect by temporarily disabling recently added XAML files to find which one lacks x:Class.","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.","Check that x:Class has a value (not x:Class=\"\") and a namespace containing at least one dot."],"exampleFix":"<!-- before (empty or missing class) -->\n<UserControl xmlns:x=\"...\" x:Class=\"\">\n  <Grid/>\n</UserControl>\n\n<!-- after -->\n<UserControl x:Class=\"MyApp.Widgets.MyControl\"\n    xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"\n    xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">\n  <Grid/>\n</UserControl>","handlingStrategy":"validation","validationCode":"# Catch empty/missing x:Class (the value must be non-empty with a namespace dot)\nGet-ChildItem -Recurse -Filter *.xaml | ForEach-Object {\n  $m = Select-String -Path $_.FullName -Pattern 'x:Class=\"([^\"]*)\"'\n  if ($m) {\n    $val = $m.Matches[0].Groups[1].Value\n    if ([string]::IsNullOrWhiteSpace($val) -or -not $val.Contains('.')) {\n      Write-Warning \"$($_.FullName): x:Class value '$val' is empty or has no namespace\"\n    }\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Because this plain Exception has no source line, keep XAML files small and add new ones one at a time to localize the culprit.","Run the error-301 x:Class validator; it pre-empts this fallback exception too.","Never author x:Class=\"\" — always a full Namespace.TypeName."],"tags":["xaml","source-generator","x-class","build","diagnostics"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}