[Bug] Xamarin.Forms UWP Entry CursorPosition and SelectionLength on Focused
#6414 aperta il 5 giu 2019
Metriche repository
- Star
- (5644 stelle)
- Metriche merge PR
- (Nessuna PR mergiata in 30 g)
Descrizione
Description
I am trying to get Select All text when I focus on a field by using Focused event and also set CursorPosition and SelectionLength for the text.
When you run the app for the first time (right after compiling) and use the Tab button on the keyboard, it will not work the first time. Once you get to the last field in the form and go back to the first field (by hitting Tab key), then select all will start to work correctly.
If you use a mouse and click into a field, the first time it will not work and the next time you click the mouse button it will correctly work and select all text. The third time you click the mouse button, it will not work again and the fourth time you click the button it will work again. Basically, if you repeatedly click the mouse button it will work every second time.
I also put debug.writeline on method focused , both that work and not work are hit thoese code.
Steps to Reproduce
- Add Entry , Add event Focused Entry1.Focused += EntryFocused; Entry2.Focused += EntryFocused; Entry3.Focused += EntryFocused; Entry4.Focused += EntryFocused;
- On Event Focus , set CursorPosition and SelectionLength private void EntryFocused(object sender, FocusEventArgs e) { Debug.WriteLine("EntryFocused Start"); try { if (sender is Entry entry) { Debug.WriteLine("Focused happen on " + entry.Text); var text = entry.Text; if (!string.IsNullOrEmpty(text)) { entry.CursorPosition = 0; entry.SelectionLength = text.Length; } Debug.WriteLine("set SelectionLength to " + text.Length); } } catch(Exception ex) { // ignored Debug.WriteLine("Exception " + ex.Message); } Debug.WriteLine("EntryFocused End"); }
- Run the app
Expected Behavior
Every time that focused happen should select all text
Actual Behavior
It will work every second time.