C# 自动选择焦点Xamarin上的所有文本

C# 自动选择焦点Xamarin上的所有文本,c#,xaml,xamarin.forms,onfocus,C#,Xaml,Xamarin.forms,Onfocus,如何在条目、编辑器、标签中自动选择焦点上的所有文本?使用跨平台 Quantity.IsFocused = true; 无工作:(在主活动添加中 public class MyEntryRenderer : EntryRenderer { public MyEntryRenderer(Context ctx) : base(ctx) {} protected override void OnElementChanged(ElementChangedEventArgs<En

如何在条目、编辑器、标签中自动选择焦点上的所有文本?使用跨平台

Quantity.IsFocused = true; 
无工作:(

在主活动添加中

public class MyEntryRenderer : EntryRenderer
{
    public MyEntryRenderer(Context ctx) : base(ctx) {}
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            var nativeEditText = (EditText)Control;
            nativeEditText.SetSelectAllOnFocus(true);
        }
    }
}

UWP自定义条目渲染器可以是

using System.ComponentModel;

using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;

[assembly: ExportRenderer(typeof(Entry), typeof(myApp.UWP.ExtendedEntryRenderer))]
namespace myApp.UWP
{
    public class ExtendedEntryRenderer : EntryRenderer
    {
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == nameof(Entry.IsFocused)
                && Control != null && Control.FocusState != Windows.UI.Xaml.FocusState.Unfocused)
                Control.SelectAll();
        }
    }
}
1.添加聚焦事件。Cs

protected  void Txt_Focussed(object sender, FocusEventArgs e)
{
    txt.CursorPosition = 0;
    txt.SelectionLength = txt.Text.Length;
}
聚焦

protected override void OnAppearing()
{
    base.OnAppearing();
    txt.Focus();
}
XAML代码

<Entry x:Name="txt" Text="155134343" Focused="Txt_Focussed" />

在我的情况下,为了让@SAIJAN KP回答有效,我让
集中处理程序
异步
,并等待一个小延迟,等待光标出现在
条目上:

    private async void Entry_Focused(object sender, FocusEventArgs e)
    {
        await Task.Delay(100);
        entryTest.CursorPosition = x;
        entryTest.SelectionLength = y;
    }
XAML


如其他答案中所述,如果您使用的是Xamarin Forms 4.2+,则可以使用CursorPosition和SelectionLength属性。但是,您需要确保在主线程上调用它,否则它将无法工作:

XAML


关于UWP呢。我如何为UWP平台创建此自定义渲染器尝试了此操作,但在实例化视图时出现反射错误。这对我很有效;`if(Control!=null){SetNativeControl(Control);Control.SetSelectAllOnFocus(true);}`@Darran-
SetNativeControl(Control)
是多余的;它本质上是说
Control=Control;
。只要做
if(Control!=null)Control.SetSelectAllOnFocus(true);
或在c#6中可以将
if
测试简化为“null检查”:简化为:
Control?.SetSelectAllOnFocus(true);
Xamarin.Form(4.2.0.709249)要求(用4.2.0.910310测试)@Dima请尝试我的回答,如其他回答中所述,对于选择文本,请确保将代码包装在
Dispatcher.BeginInvokeOnMainThread
中,否则它将不起作用。另一个需要注意的是,如果用户在渲染后很早点击它,则
txt.text
可能为空。请确保处理该代码以防止崩溃。它不起作用对我来说,它只是将光标放在末尾,但未选中的
MyEntry中的文本。如果用户在渲染后很早点击它,文本
中的文本可能为空。请确保对此进行处理以防止崩溃。它对我有效,在我的情况下,它适用于任何条目。
    private async void Entry_Focused(object sender, FocusEventArgs e)
    {
        await Task.Delay(100);
        entryTest.CursorPosition = x;
        entryTest.SelectionLength = y;
    }
<Entry x:Name="entryTest" Text="155134343" Focused="Entry_Focused"  />
<Entry x:Name="MyEntry" Focused="MyEntry_Focused"  />
private void MyEntry_Focused(object sender, FocusEventArgs e)
{
    Dispatcher.BeginInvokeOnMainThread(() =>
    {
        MyEntry.CursorPosition = 0;
        MyEntry.SelectionLength = MyEntry.Text != null ? MyEntry.Text.Length : 0
    });
}