xamlxamarinxamarin.formsattached-properties

Apply an attached property to Xamarin Entry and Editor to force CAPITALIZATION and set its Style in Application.Resources


I have been searching around trying to find a way to create an Attached Property almost exactly like this one Is It possible to set a Keyboard to "Sentence Capitalisation" in XAML for an Entry? However I cannot figure out how to implement it. Currently I am creating the KeyboardStyles class in a Controls folder under the common project. Should it be in the Android & iOS project? Should it be in the code behind? ViewModel? I've been trying everything with no luck. My ultimate goal is to apply this setter (or something similar) in my global Styles:

                <Setter Property="local:KeyboardStyle.KeyboardFlags"
        Value="Suggestions,CapitalizeCharacter"

Otherwise I'll have to visit every control and set it about 100 times.


Solution

  • I tested the code the answer of the case provided and it's missing accessors: Get and Set method:

    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 string GetKeyboardFlags(InputView view)
        {
            return (string)view.GetValue(KeyboardFlagsProperty);
        }
        public static void SetKeyboardFlags(InputView view, string value)
        {
            view.SetValue(KeyboardFlagsProperty, value);
        }
    
        ...
    
    }
    

    Then you can consume it in App.xaml:

    <Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:forms" 
                 x:Class="forms.App">
        <Application.Resources>
            <Style TargetType="Entry">
                <Setter Property="local:KeyboardStyle.KeyboardFlags" 
                        Value="Spellcheck,CapitalizeSentence"/>
            </Style>
        </Application.Resources>
    </Application>
    

    It works well.

    For more details, you can refer to Create accessors of Attached Properties by official.