Search code examples
.netxamarin.formsmaui

URANIUM UI Validations loosing its focus while typing


I am following this page to add URANIUM UI Validations for my MAUI 8.0 project. It displays a TextField as expected but while typing the name it is losing its focus.

I created a new project and added the necessary NuGet packages and here is my XAML code

xmlns:material="http://schemas.enisn-projects.io/dotnet/maui/uraniumui/material"
xmlns:validations="clr-namespace:InputKit.Shared.Validations;assembly=InputKit.Maui"

<material:TextField Title="Enter Name">
    <validations:MinLengthValidation MinLength="3" />
</material:TextField>

On typing first char it displays the error label "The field should contain at least 3 characters" and loses its focus on TextFiled. I manually need to click on TextField and then have to type another char. Now, on typing the third char, it clears the error label and again loses its focus on TextFiled so, I now again manually click on TextFiled to enter further characters.

I tried with ValidationBinding too but I see the same behavior.

Text="{v:ValidationBinding Form.Email, Mode=TwoWay}"

Am I missing something here?


Solution

  • Edit: This problem was solved in v2.8 with issue #373


    The following section is a workaround only for v2.7 and below:

    According to my research, WinUI panels are focusable unlike other platforms. When a panel is focused it automatically delivers the focus event to its children recursively. It's even triggered when the layout is changed.

    Here is a workaround for Windows:

    public class MyTextField : TextField
    {
    #if WINDOWS
        protected override void RegisterForEvents()
        {
            base.RegisterForEvents();
    
            this.Unfocused += OnUnfocused;
        }
    
        protected override void ReleaseEvents()
        {
            base.ReleaseEvents();
            this.Unfocused -= OnUnfocused;
        }
    
        protected override void CheckAndShowValidations()
        {
            // Leave empty
        }
    
        private void OnUnfocused(object sender, FocusEventArgs e)
        {
            base.CheckAndShowValidations();
        }
    #endif
    }
    
    <local:MyTextField />
    

    Unfortunately, there is no active solution right now, I'll update here when a solution is applied.

    It can be tracked from the original issue from here: https://github.com/enisn/UraniumUI/issues/373#issuecomment-1893096078