最后我通过为文本框和文本区域(编辑器)创建自定义渲染实现了这一点,下面的代码对我有用。
在表单中,我创建了这个类。
using Xamarin.Forms;
namespace CustomRenderer
{
public class MyEntry : Entry
{
}
}
在 android 中,我为 MyEntry 创建了自定义渲染。
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using CustomRenderer;
using CustomRenderer.Android;
[assembly: ExportRenderer (typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.Android
{
class MyEntryRenderer : EntryRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged (e);
if (Control != null) {
Control.SetBackgroundColor (global::Android.Graphics.Color.LightGreen);
Control.PrivateImeOptions = "nm";
}
}
}
}
iOS 下的渲染图。
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using UIKit;
using CustomRenderer;
using CustomRenderer.iOS;
[assembly: ExportRenderer (typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.iOS
{
public class MyEntryRenderer : EntryRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged (e);
if (Control != null) {
// do whatever you want to the UITextField here!
Control.BackgroundColor = UIColor.FromRGB (204, 153, 255);
Control.BorderStyle = UITextBorderStyle.Line;
Control.KeyboardType = UIKeyboardType.EmailAddress;
}
}
}
}
在 xaml 中,我有以下语法。
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CustomRenderer;assembly=CustomRenderer"
x:Class="CustomRenderer.MainPageXaml">
<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<Label Text="Hello, Custom Renderer!" />
<local:MyEntry Text="In Shared Code" />
</StackLayout>
</ContentPage>
通过上述方法,我已成功禁用 iOS 和 Android 键盘上的听写按钮。