xamarin/Xamarin.Forms

[Bug] Xamarin.Forms UWP Entry CursorPosition and SelectionLength on Focused

开放

#6,414 创建于 2019年6月5日

 (0 条评论) (0 个反应) (0 位负责人)C# (1,926 个派生)batch import
e/3 :clock3:help wantedinactivep/UWPt/bug :bug:up-for-grabs

仓库指标

星标
 (5,644 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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

  1. Add Entry , Add event Focused Entry1.Focused += EntryFocused; Entry2.Focused += EntryFocused; Entry3.Focused += EntryFocused; Entry4.Focused += EntryFocused;
  2. 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"); }
  3. Run the app

Expected Behavior

Every time that focused happen should select all text

Actual Behavior

It will work every second time.

Screenshots

video.zip

Reproduction Link

EntryFocusCode.zip

贡献者指南