我知道Xamarin.Android代码,但是Xamarin.IOS呢?
Xamarin.Android代码:
public class CustomNumEntry : EntryRenderer
{
public CustomNumEntry(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (this.Control != null)
{
Control.SetPadding(10, 0, 0, 15);
Control.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagSigned | Android.Text.InputTypes.NumberFlagDecimal;
Control.TextAlignment = Android.Views.TextAlignment.Center;
}
if (e.OldElement == null)
{
var nativeEditText = (global::Android.Widget.EditText)Control;
nativeEditText.SetSelectAllOnFocus(true);
}
}
}
答案 0 :(得分:0)
对于Xamarin iOS:
nativeTextField.EditingDidBegin += (object sender, EventArgs eIos) =>
{
nativeTextField.PerformSelector(new Selector("selectAll"), null, 0.0f);
};
使用自定义渲染器(适用于iOS)的Xamarin.Forms(您正在寻找):
public class CustomNumEntry : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var nativeTextField = (UITextField)Control;
nativeTextField.EditingDidBegin += (object sender, EventArgs eIos) =>
{
nativeTextField.PerformSelector(new Selector("selectAll"), null, 0.0f);
};
}
}
}
答案 1 :(得分:0)
解决方案:
您可以在方法 EditingStarted
中实现它。请参考以下代码
namespace xxx.iOS
{
public class MyEntryRenderer:EntryRenderer,IUITextFieldDelegate
{
public MyEntryRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
Control.WeakDelegate = this;
}
}
[Export("textFieldDidBeginEditing:")]
public void EditingStarted(UITextField textField)
{
textField.PerformSelector(new Selector("selectAll:"), null, 0.0f);
}
}
}