The manual docs about defining attributes on custom subclasses of VisualElement contains errors that make it hard to get started. They include this example:
class StatusBar : VisualElement
{
public new class Factory : UxmlFactory<StatusBar, UxmlTraits> {}
public new class Traits : base.UxmlTraits
{
UxmlStringAttributeDescription m_Status = new UxmlStringAttributeDescription { name = "status" };
public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
{
get { yield break; }
}
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((StatusBar)ve).status = m_Status.GetValueFromBag(bag, cc);
}
}
// ...
}
Line 3 should refer to Traits instead of UxmlTraits:
public new class Factory : UxmlFactory<StatusBar, UxmlTraits> {} // <-- WRONG
public new class Factory : UxmlFactory<StatusBar, Traits> {} // <-- RIGHT
… so that the newly declared inner Traits class is actually used.
It also uses tries to refer to the base class as “base.UxmlTraits” for some reason, which is a compiler error. Removing “.base” fixes that.