{"record":{"id":"234fda5bd831e247","repo":"unoplatform/uno","slug":"unable-to-find-class-name-for-toplevel-control","errorCode":null,"errorMessage":"Unable to find class name for toplevel control","messagePattern":"Unable to find class name for toplevel control","errorType":"exception","errorClass":"XamlGenerationException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs","lineNumber":2345,"sourceCode":"\t\tprivate XamlMemberDefinition GetMember(XamlObjectDefinition xamlObjectDefinition, string memberName)\n\t\t{\n\t\t\tvar member = FindMember(xamlObjectDefinition, memberName);\n\n\t\t\tif (member == null)\n\t\t\t{\n\t\t\t\tthrow new XamlGenerationException($\"'{memberName}' is not defined on '{xamlObjectDefinition.Type.Name}'\", xamlObjectDefinition);\n\t\t\t}\n\n\t\t\treturn member;\n\t\t}\n\n\t\tprivate XClassName GetClassName(XamlObjectDefinition control)\n\t\t{\n\t\t\tvar classMember = FindClassName(control);\n\n\t\t\tif (classMember == null)\n\t\t\t{\n\t\t\t\tthrow new XamlGenerationException(\"Unable to find class name for toplevel control\", control);\n\t\t\t}\n\n\t\t\treturn classMember;\n\t\t}\n\n\t\tprivate XClassName? FindClassName(XamlObjectDefinition control)\n\t\t{\n\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\n\t\t\t\tvar index = fullName.LastIndexOf('.');\n\n\t\t\t\treturn new(fullName.Substring(0, index), fullName.Substring(index + 1), _metadataHelper.FindTypeByFullName(fullName) as INamedTypeSymbol);\n\t\t\t}\n\t\t\telse","sourceCodeStart":2327,"sourceCodeEnd":2363,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs#L2327-L2363","documentation":"Thrown by GetClassName() when the top-level (root) XAML element has no x:Class attribute. GetClassName is called at line 374 for every non-ResourceDictionary root element; it splits the x:Class value into namespace and class name to generate the partial class. Without x:Class the generator cannot synthesize the code-behind partial class, so generation aborts. This is an XamlGenerationException rooted at the top-level control node.","triggerScenarios":"A Page, UserControl, or Window XAML file whose root element omits x:Class=\"MyApp.MyPage\"; the x:Class attribute is present but on a non-root element; the root uses a custom xmlns that maps x: to a different namespace so the generator does not recognize the Class member.","commonSituations":"Creating a XAML file from a template that strips x:Class; renaming the code-behind class without updating XAML; mixing up a loose ResourceDictionary XAML (which intentionally has no x:Class) with a Page XAML; a broken xmlns:x declaration.","solutions":["Add x:Class=\"YourNamespace.YourClass\" to the root element matching the code-behind partial class.","Verify the xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" declaration is present on the root and unmodified.","If the file is meant to be a standalone ResourceDictionary root, confirm it is being treated as one (ResourceDictionary as root) — otherwise it needs x:Class.","Ensure the namespace in x:Class matches the code-behind file's namespace exactly."],"exampleFix":"<!-- before -->\n<Page\n    xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">\n  <Grid/>\n</Page>\n\n<!-- after -->\n<Page\n    x:Class=\"MyApp.Views.MyPage\"\n    xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"\n    xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">\n  <Grid/>\n</Page>","handlingStrategy":"validation","validationCode":"# Verify every Page/UserControl/Window XAML root has x:Class\nGet-ChildItem -Recurse -Filter *.xaml | Where-Object {\n  (Select-String -Path $_.FullName -Pattern '<(Page|UserControl|Window|ContentDialog)' -List) -and\n  -not (Select-String -Path $_.FullName -Pattern 'x:Class=' -List)\n} | ForEach-Object { Write-Warning \"$($_.FullName): root element missing x:Class\" }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the Uno item templates (dotnet new unopage / unousercontrol) which inject x:Class automatically.","When renaming a code-behind class, update x:Class in the same commit.","Treat a missing x:Class on a non-dictionary root as a build-breaking typo, not a style preference."],"tags":["xaml","source-generator","x-class","build"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}