最後,我已經通過創建自定義渲染文本框和文本區(編者),下面的代碼爲我工作實現了這個本地平臺的方法。
在表格中,我創建了這個類。
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鍵盤。
您需要使用自定義渲染器,因爲鍵盤是特定於平臺的。 – BytesGuy
謝謝你的回覆,是的,我做到了,我創建了自定義渲染器,它適用於帶有PrivateImeOptions =「nm」屬性的android,現在嘗試iOS,我不確定應該使用哪個入口屬性來禁用聽寫按鈕 –