Is it possible to set the keyboard to the “sentence spell” in XAML to enter?

I watched Xamarin.Forms: specifying additional keyboard options

And I saw this piece of code for setting the keyboard icon in the "Assignment of capital"

Content = new StackLayout
{
    Padding = new Thickness(0, 20, 0, 0),
    Children =
    {
        new Entry
        {
          Keyboard = Keyboard.Create(KeyboardFlags.CapitalizeSentence)
        }
    }
};

It looks great and I would like to use it in XAML.

Can this be done in XAML?

+4
source share
2 answers

As the first answer correctly says, setting keyboard flags is not possible out of the box. Although you can highlight a subclass Entry, there is a more elegant way by creating an attached property :

public class KeyboardStyle
    {
        public static BindableProperty KeyboardFlagsProperty = BindableProperty.CreateAttached(
            propertyName: "KeyboardFlags",
            returnType: typeof(string),
            declaringType: typeof(InputView),
            defaultValue: null,
            defaultBindingMode: BindingMode.OneWay,
            propertyChanged: HandleKeyboardFlagsChanged);

        public static void HandleKeyboardFlagsChanged(BindableObject obj, object oldValue, object newValue)
        {
            var entry = obj as InputView;

            if(entry == null)
            {
                return;
            }

            if(newValue == null)
            {
                return;
            }

            string[] flagNames = ((string)newValue).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            KeyboardFlags allFlags = 0;

            foreach (var flagName in flagNames) {
                KeyboardFlags flags = 0;
                Enum.TryParse<KeyboardFlags>(flagName.Trim(), out flags);
                if(flags != 0)
                {
                    allFlags |= flags;      
                }
            }

            Debug.WriteLine("Setting keyboard to: " + allFlags);
            var keyboard = Keyboard.Create(allFlags);

            entry.Keyboard = keyboard;
        }
    }

XAML ( local):

<?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:KeyboardTest"
        x:Class="KeyboardTest.KeyboardTestPage">

    <Entry x:Name="entry" Text="Hello Keyboard" local:KeyboardStyle.KeyboardFlags="Spellcheck,CapitalizeSentence"/>
</ContentPage>

, :

<Style TargetType="Entry">
    <Setter Property="local:KeyboardStyle.KeyboardFlags"
            Value="Spellcheck,CapitalizeSentence"/>
</Style>
+6

XAML.

.

KeyboardFlags.

Entry .

+1

All Articles