xamarin/Xamarin.Forms

Unfocused issue with Entry in listview

Open

#1,906 opened on Feb 17, 2018

View on GitHub
 (8 comments) (0 reactions) (0 assignees)C# (1,926 forks)batch import
a/listviewe/4 :clock4:help wantedm/high impact :black_large_square:p/Androidt/bug :bug:up-for-grabs

Repository metrics

Stars
 (5,644 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Description

I have a ListView with ViewCells in my ContentPage. Each ViewCell has a CustomEntry (that I have created). I want control if the user leaves the Entry empty. If he leaves the Entry empty, I would like to leave the last value that Entry has had. The easiest solution for me is using Focused and Unfocused events. But the behaviour between events is very strange.

Steps to Reproduce

Tab first CustomEntry Tab second CustomEntry Press Del key of the keyboard You will see that the cursor moves at the beginning of the CustomEntry but it doesn't remove the text.

Expected Behavior

Tap first CustomEntry Fires Focused first CustomEntry event Fires Unfocused first CustomEntry event Tap second CustomEntry Fires Focused second CustomEntry event Fires Unfocused second CustomEntry event

Actual Behavior

Tap first CustomEntry Fires Focused first CustomEntry event Fires Unfocused first CustomEntry event Tap second CustomEntry Press Del key of the keyboard Fires Focused second CustomEntry event Fires Unfocused second CustomEntry event Fires Focused first CustomEntry event Fires Unfocused first CustomEntry event Fires Focused second CustomEntry event The cursor moves at the beginning of the CustomEntry

Basic Information

  • Version with issue: 2.5

  • Last known good version: Don't know

  • IDE: VS2017

  • Platform Target Frameworks:

    • Android: 8.0
  • Affected Devices: All Android

Code

{
    private string oldValue;

    public CustomEntry()
    {
        this.oldValue = "1";            
        this.Focused += CustomEntry_Focused;
        this.Unfocused += CustomEntry_Unfocused;                        
    }

    private void CustomEntry_Focused(object sender, FocusEventArgs e)
    {
        if (this.Text != null)
        {
            this.oldValue = this.Text;
        }  
    }

    private void CustomEntry_Unfocused(object sender, FocusEventArgs e)
    {
        try
        {
            if (this.Text != null) { 
                //If the user leaves the field empty                
                if (this.Text.Trim().Equals(string.Empty))
                {
                    this.Text = this.oldValue;
                }
            }
        }
        catch (FormatException ex) { }
    }
}```

Contributor guide