Disable autocomplete in Xamarin.Forms PCL XAML Page
Asked Answered
O

5

15

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" />
Ortego answered 17/11, 2014 at 5:54 Comment(0)
P
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.

Privet answered 17/11, 2014 at 17:12 Comment(1)
@Ortego The documentation doesn't appear to have been updated, but there is a None member now. github.com/xamarin/Xamarin.Forms/blob/…Musket
M
33

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}" />
Musket answered 7/9, 2016 at 20:3 Comment(1)
It would be wonderful if this could be incorporated into a style?Spherical
I
2

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.

Iloilo answered 7/12, 2015 at 13:7 Comment(1)
Creating resources for static members is not necessary. See my answer for details on the x:Static attribute.Musket
P
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.

Privet answered 17/11, 2014 at 17:12 Comment(1)
@Ortego The documentation doesn't appear to have been updated, but there is a None member now. github.com/xamarin/Xamarin.Forms/blob/…Musket
H
1

The KeyboardFlags should do it, something like:

MyEntry.Keyboard = Keyboard.Create(KeyboardFlags.CapitalizeSentence | KeyboardFlags.Spellcheck);
Hemo answered 1/2, 2015 at 21:11 Comment(2)
In a way, yes. You will have to programmatically create resources in App.OnStart, and reach them as StaticResource, with binding. I'll add an answer in a moment.Iloilo
@Julien See my answer for how to create custom keyboards using XAML only.Musket
B
0

I know this is an old thread, but I found a way to do this in styles.

I have all of my styles defined in a ResourceDictionary in my App.xaml. There I can do things like:

<Style x:Key="StyleEntryNumeric" TargetType="Entry">
    <Setter Property="ClearButtonVisibility" Value="WhileEditing" />
    <Setter Property="Keyboard" Value="Numeric" />
</Style>

and then use it like:

<Entry Style="{DynamicResource StyleEntryNumeric}" />

But, what if I want to set KeyboardFlags? Well, I did that with a bit of code in my App.xaml.cs and a simple binding. First, in the top of my App.xaml:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        x:Name="ThisControl"
        x:Class="App.Forms.App">

Then where my styles are in the same App.xaml:

<Style x:Key="StyleEntryText" TargetType="Entry">
    <Setter Property="ClearButtonVisibility" Value="WhileEditing" />
    <Setter Property="Keyboard" Value="{Binding Source={x:Reference ThisControl}, Path=KeyboardText}" />
</Style>

Finally, in my App.xaml.cs code behind:

public Keyboard KeyboardText
    => Keyboard.Create(KeyboardFlags.CapitalizeNone | KeyboardFlags.Spellcheck);

And, I can create other similar properties in my code behind to use for other styles if needed.

Buiron answered 17/3, 2020 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.