Disable autocomplete in Xamarin.Forms PCL XAML Page

余生长醉 提交于 2020-01-01 04:40:06

问题


I have a PCL which stores my MVVM pages in XAML. I have the following in the XAML file, but I'd like to disable the autocomplete feature on the keyboard. Does anyone know how I can do this in the XAML?

<Entry Text="{Binding Code}" Placeholder="Code" />

回答1:


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.




回答2:


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}" />



回答3:


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.




回答4:


The KeyboardFlags should do it, something like:

MyEntry.Keyboard = Keyboard.Create(KeyboardFlags.CapitalizeSentence | KeyboardFlags.Spellcheck);


来源:https://stackoverflow.com/questions/26966404/disable-autocomplete-in-xamarin-forms-pcl-xaml-page

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