unoplatform/uno · error · XamlGenerationException
Unable to find class name for toplevel control
Error message
Unable to find class name for toplevel control
What it means
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.
Source
Thrown at src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs:2345
private XamlMemberDefinition GetMember(XamlObjectDefinition xamlObjectDefinition, string memberName)
{
var member = FindMember(xamlObjectDefinition, memberName);
if (member == null)
{
throw new XamlGenerationException($"'{memberName}' is not defined on '{xamlObjectDefinition.Type.Name}'", xamlObjectDefinition);
}
return member;
}
private XClassName GetClassName(XamlObjectDefinition control)
{
var classMember = FindClassName(control);
if (classMember == null)
{
throw new XamlGenerationException("Unable to find class name for toplevel control", control);
}
return classMember;
}
private XClassName? FindClassName(XamlObjectDefinition control)
{
var classMember = control.Members.FirstOrDefault(m => m.Member.Name == "Class");
if (classMember?.Value != null)
{
var fullName = classMember.Value.ToString() ?? "";
var index = fullName.LastIndexOf('.');
return new(fullName.Substring(0, index), fullName.Substring(index + 1), _metadataHelper.FindTypeByFullName(fullName) as INamedTypeSymbol);
}
elseView on GitHub (pinned to 0418340488)
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.
Example fix
<!-- before -->
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid/>
</Page>
<!-- after -->
<Page
x:Class="MyApp.Views.MyPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid/>
</Page> Defensive patterns
Strategy: validation
Validate before calling
# Verify every Page/UserControl/Window XAML root has x:Class
Get-ChildItem -Recurse -Filter *.xaml | Where-Object {
(Select-String -Path $_.FullName -Pattern '<(Page|UserControl|Window|ContentDialog)' -List) -and
-not (Select-String -Path $_.FullName -Pattern 'x:Class=' -List)
} | ForEach-Object { Write-Warning "$($_.FullName): root element missing x:Class" } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unable to find x:Class on the top level element
- '{memberName}' is not defined on '{xamlObjectDefinition.Type
- Unable to implicitly initialize collection for '{contentProp
- Unable to find the type '{_xClassName?.Namespace}.{_xClassNa
- The type '{fullyQualifiedMetadataName}' is not found.
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/234fda5bd831e247.
Report an issue: GitHub.