自动 select 聚焦 Xamarin 上的所有文本
Automatically select all text on focus Xamarin
如何自动select Entry、Editor、Label 中的所有文本都成为焦点?使用跨平台。
Quantity.IsFocused = true;
没有工作:(
在MainActivity中添加
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);
}
}
}
并在顶部添加:
[assembly: ExportRenderer (typeof (Entry), typeof (MyEntryRenderer))]
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.Add 专注 Event.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 回答有效,我制作了 Focused
处理程序 async
并等待一小段延迟以等待光标出现在 Entry
上:
private async void Entry_Focused(object sender, FocusEventArgs e)
{
await Task.Delay(100);
entryTest.CursorPosition = x;
entryTest.SelectionLength = y;
}
XAML
<Entry x:Name="entryTest" Text="155134343" Focused="Entry_Focused" />
如其他答案中所述,如果您使用的是 Xamarin Forms 4.2+,则可以使用 CursorPosition 和 SelectionLength 属性。但是,您需要确保在 Main/UI 线程上调用,因为必须在那里完成对 UI 元素的所有直接操作。如果不这样做,应用程序很可能会在部署到设备时崩溃,即使它在模拟器中看起来 运行 正常。
XAML
<Entry x:Name="MyEntry" Focused="MyEntry_Focused" />
C#
private void MyEntry_Focused(object sender, FocusEventArgs e)
{
Dispatcher.BeginInvokeOnMainThread(() =>
{
MyEntry.CursorPosition = 0;
MyEntry.SelectionLength = MyEntry.Text != null ? MyEntry.Text.Length : 0
});
}
对于 Xamarin.Forms,您还可以使用 Device.BeginInvokeOnMainThread() rather than Dispatcher. If you're using Xamarin Essentials, there is also MainThread.BeginInvokeOnMainThread()(与 Device.BeginInvokeOnMainThread() 的作用相同)。
Xamarin.Forms has a method called Device.BeginInvokeOnMainThread(Action) that does the same thing as MainThread.BeginInvokeOnMainThread(Action). While you can use either method in a Xamarin.Forms app, consider whether or not the calling code has any other need for a dependency on Xamarin.Forms. If not, MainThread.BeginInvokeOnMainThread(Action) is likely a better option.
@kiddailey posted ,这是一个基于此的可重用解决方案:
在代码隐藏中创建行为
public class SelectContentWhenFocusedBehavior : Behavior<Entry>
{
protected override void OnAttachedTo(Entry entry)
{
entry.Focused += OnFocused;
base.OnAttachedTo(entry);
}
protected override void OnDetachingFrom(Entry entry)
{
entry.Focused -= OnFocused;
base.OnDetachingFrom(entry);
}
private static void OnFocused(object sender, FocusEventArgs args)
{
Device.BeginInvokeOnMainThread(() =>
{
Entry myEntry = sender as Entry;
myEntry.CursorPosition = 0;
myEntry.SelectionLength = myEntry.Text != null ? myEntry.Text.Length : 0;
});
}
}
在您的视图中将 link 添加到行为命名空间
xmlns:behav="clr-namespace:MyApp.Resources.Behaviors"
在视图中为您的条目添加行为
<Entry Text="{Binding something, Mode=TwoWay}" Keyboard="Numeric">
<Entry.Behaviors>
<behav:SelectContentWhenFocusedBehavior/>
</Entry.Behaviors>
</Entry>
如何自动select Entry、Editor、Label 中的所有文本都成为焦点?使用跨平台。
Quantity.IsFocused = true;
没有工作:(
在MainActivity中添加
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);
}
}
}
并在顶部添加:
[assembly: ExportRenderer (typeof (Entry), typeof (MyEntryRenderer))]
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.Add 专注 Event.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 回答有效,我制作了 Focused
处理程序 async
并等待一小段延迟以等待光标出现在 Entry
上:
private async void Entry_Focused(object sender, FocusEventArgs e)
{
await Task.Delay(100);
entryTest.CursorPosition = x;
entryTest.SelectionLength = y;
}
XAML
<Entry x:Name="entryTest" Text="155134343" Focused="Entry_Focused" />
如其他答案中所述,如果您使用的是 Xamarin Forms 4.2+,则可以使用 CursorPosition 和 SelectionLength 属性。但是,您需要确保在 Main/UI 线程上调用,因为必须在那里完成对 UI 元素的所有直接操作。如果不这样做,应用程序很可能会在部署到设备时崩溃,即使它在模拟器中看起来 运行 正常。
XAML
<Entry x:Name="MyEntry" Focused="MyEntry_Focused" />
C#
private void MyEntry_Focused(object sender, FocusEventArgs e)
{
Dispatcher.BeginInvokeOnMainThread(() =>
{
MyEntry.CursorPosition = 0;
MyEntry.SelectionLength = MyEntry.Text != null ? MyEntry.Text.Length : 0
});
}
对于 Xamarin.Forms,您还可以使用 Device.BeginInvokeOnMainThread() rather than Dispatcher. If you're using Xamarin Essentials, there is also MainThread.BeginInvokeOnMainThread()(与 Device.BeginInvokeOnMainThread() 的作用相同)。
Xamarin.Forms has a method called Device.BeginInvokeOnMainThread(Action) that does the same thing as MainThread.BeginInvokeOnMainThread(Action). While you can use either method in a Xamarin.Forms app, consider whether or not the calling code has any other need for a dependency on Xamarin.Forms. If not, MainThread.BeginInvokeOnMainThread(Action) is likely a better option.
@kiddailey posted
在代码隐藏中创建行为
public class SelectContentWhenFocusedBehavior : Behavior<Entry> { protected override void OnAttachedTo(Entry entry) { entry.Focused += OnFocused; base.OnAttachedTo(entry); } protected override void OnDetachingFrom(Entry entry) { entry.Focused -= OnFocused; base.OnDetachingFrom(entry); } private static void OnFocused(object sender, FocusEventArgs args) { Device.BeginInvokeOnMainThread(() => { Entry myEntry = sender as Entry; myEntry.CursorPosition = 0; myEntry.SelectionLength = myEntry.Text != null ? myEntry.Text.Length : 0; }); } }
在您的视图中将 link 添加到行为命名空间
xmlns:behav="clr-namespace:MyApp.Resources.Behaviors"
在视图中为您的条目添加行为
<Entry Text="{Binding something, Mode=TwoWay}" Keyboard="Numeric"> <Entry.Behaviors> <behav:SelectContentWhenFocusedBehavior/> </Entry.Behaviors> </Entry>