Disable autocomplete in Xamarin.Forms PCL XAML Page

时间秒杀一切 提交于 2019-12-03 12:15:59

Forms supports a KeyboardFlags.Suggestion enum which I assume is intended to control this behavior, but it doesn't appear to be very well documented.

Taylor Buchanan

Custom Keyboard instances can be created in XAML using the x:FactoryMethod attribute. What you're wanting can be achieved with the following markup:

<Entry Text="{Binding Code}" Placeholder="Code">
  <Entry.Keyboard>
    <Keyboard x:FactoryMethod="Create">
      <x:Arguments>
        <KeyboardFlags>None</KeyboardFlags>
      </x:Arguments>
    </Keyboard>
  </Entry.Keyboard>
</Entry>

KeyboardFlags.None removes all special keyboard features from the field.

Multiple enums can be specified in XAML by separating them with a comma:

<KeyboardFlags>CapitalizeSentence,Spellcheck</KeyboardFlags>

When you don't need a custom Keyboard, you can use one of the predefined ones by taking advantage of the x:Static attribute:

<Entry Placeholder="Phone" Keyboard="{x:Static Keyboard.Telephone}" />

While there's already an answer, I thought I'd elaborate a bit further about usage in XAML.

Unlike in code-behind, you cannot create a new instance of the Keyboard class to be used, but there IS a way. Hopefully you're already xaml-ified your App.cs (remove it, and create App.xaml and App.xaml.cs), that way you don't have to check if the Resources property has been initialized yet.

The next step is to override the OnStart() method, and add the proper entries for the various keyboards you use. I usually use three keyboards: numeric, e-mail and text. Another useful one is the Url keyboard, but you can add it the same way.

protected override void OnStart()
{
    base.OnStart();
    this.Resources.Add("KeyboardEmail", Keyboard.Email);
    this.Resources.Add("KeyboardText", Keyboard.Text);
    this.Resources.Add("KeyboardNumeric", Keyboard.Numeric);
}

This little code will make the keyboards available as static resources. To use them in XAML, just do the following:

<Entry x:Name="emailEntry" Text="{Binding EMail}" Keyboard="{StaticResource KeyboardEmail}" />

And voilá, your entry now has an e-mail keyboard.

The KeyboardFlags should do it, something like:

MyEntry.Keyboard = Keyboard.Create(KeyboardFlags.CapitalizeSentence | KeyboardFlags.Spellcheck);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!