Bindings error with custom Textfields

Hello,
I have a problem with a custom class I found here: Changing caret style (inside a TextField)?

Here it is :

using System;

namespace UnityEngine.UIElements
{
    public class BlinkingTextField : TextField
    {
        private readonly IVisualElementScheduledItem blink;

        private long blinkInterval = 500;
        private bool isBlinkEnabled = true;
        private string blinkStyle = "cursor-transparent";

        /// <summary>
        /// Caret blink interval in ms.
        /// </summary>
        public long BlinkInterval
        {
            get => blinkInterval;
            set
            {
                blinkInterval = value;
                blink?.Every(blinkInterval);
            }
        }

        /// <summary>
        /// Caret uss style applied on blink.
        /// </summary>
        public string BlinkStyle
        {
            get => blinkStyle;
            set => blinkStyle = value;
        }

        /// <summary>
        /// If true, caret blinks.
        /// </summary>
        public bool BlinkEnable
        {
            get => isBlinkEnabled;
            set
            {
                if (isBlinkEnabled == value)
                    return;

                isBlinkEnabled = value;

                if (!isBlinkEnabled)
                {
                    if (IsFocused)
                        blink?.Pause();

                    if (ClassListContains(blinkStyle))
                        RemoveFromClassList(blinkStyle);
                }
                else if (IsFocused)
                {
                    blink?.Resume();
                }
            }
        }

        /// <summary>
        /// Returns true if active input.
        /// </summary>
        bool IsFocused => focusController?.focusedElement == this;

        public BlinkingTextField()
        {
            RegisterCallback<FocusEvent>(OnFocus);
            RegisterCallback<BlurEvent>(OnInputEnded);

            blink = schedule.Execute(() =>
            {
                if (ClassListContains(blinkStyle))
                    RemoveFromClassList(blinkStyle);
                else
                    AddToClassList(blinkStyle);
            }).Every(blinkInterval);

            blink.Pause();
        }

        private void OnFocus(FocusEvent evt)
        {
            if (!isBlinkEnabled)
                return;

            blink.Resume();
        }

        private void OnInputEnded(BlurEvent evt)
        {
            blink.Pause();
        }
        
        [Obsolete("Obsolete")]
        public new class UxmlFactory : UxmlFactory<BlinkingTextField, BlinkingUxmlTraits> { }
        
        [Scripting.Preserve]
        [Obsolete("Obsolete")]
        public class BlinkingUxmlTraits : UxmlTraits
        {
            private readonly UxmlLongAttributeDescription blinkInterval = new()
                { name = "blink-interval", use = UxmlAttributeDescription.Use.Optional, defaultValue = 500 };

            private readonly UxmlBoolAttributeDescription blinkEnable = new()
                { name = "blink-enable", use = UxmlAttributeDescription.Use.Optional, defaultValue = true };

            private readonly UxmlStringAttributeDescription blinkStyle = new()
            {
                name = "blink-style", use = UxmlAttributeDescription.Use.Optional, defaultValue = "cursor-transparent"
            };

            public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
            {
                base.Init(ve, bag, cc);
                ((BlinkingTextField)ve).BlinkInterval = blinkInterval.GetValueFromBag(bag, cc);
                ((BlinkingTextField)ve).BlinkEnable = blinkEnable.GetValueFromBag(bag, cc);
                ((BlinkingTextField)ve).BlinkStyle = blinkStyle.GetValueFromBag(bag, cc);
            }
        }
    }
}

I replaced this code in my .uxml which worked fine:

<engine:TextField label=β€œMail address” placeholder-text=” john@mail.com β€˜ keyboard-type=’ EmailAddress β€˜ hide-placeholder-on-focus=’ true β€˜ name=’ MailAddressInput β€˜ class=’ frama-text frama-input β€˜ style=’ flex-wrap: nowrap; flex-direction: column; ”>
    <Bindings>
        <engine:DataBinding property=” value β€˜ binding-mode=’ TwoWay β€˜ data-source-path=’ mailAddress β€˜ data-source-type=’ LoginPanelManager, Assembly-CSharp ” />
    </Bindings>
</engine:TextField>

By:

<engine:BlinkingTextField label=β€œMail address” placeholder-text=” john@mail.com β€˜ keyboard-type=’ EmailAddress β€˜ hide-placeholder-on-focus=’ true β€˜ name=’ MailAddressInput β€˜ class=’ frama-text frama-input β€˜ style=’ flex-wrap: nowrap; flex-direction: column; ”>
    <Bindings>
        <engine:DataBinding property=” value β€˜ binding-mode=’ TwoWay β€˜ data-source-path=’ mailAddress β€˜ data-source-type=’ LoginPanelManager, Assembly-CSharp ” />
    </Bindings>
 </engine:BlinkingTextField>

The caret blinks well but I have this error popping up:

Element 'Bindings' is missing a UxmlElementAttribute and has no registered factory method. Please ensure that you have the correct namespace imported.

Does anyone know how to solve this?
Thanks!

Hello!

You need to ensure your element is bindable by adding the UxmlElement attribute and making it partial:

    [UxmlElement]
    public partial class BlinkingTextField : TextField

Hope this helps!

1 Like

Oh, it works! I didn’t know that, you’ve saved my day!
Thank you!

I’m really glad! Have a great day! Let me know if you have any other questions!