Search code examples
c#xamarinxamarin.formsmonkeypatching

Xamarin.Forms - possible to set an element property globally?


Say I want an Entry element to not have auto-correct or auto-capitalize. This can be done by setting its Keyboard property like so:

new Entry { Keyboard = Keyboard.Create(0) }

Now, how do I make that the global default for all Entry elements?

I know I can create a custom element that inherits from the built-in element, and override the property that way, like:

public class EntryCustom : Entry
{
    public EntryCustom()
    {
        Keyboard = Keyboard.Create(0);
    }
}

and then simply call it like:

new EntryCustom { ... }

But is there a way to do this directly on the built-in element type, without creating a custom element type?


Solution

  • You can do this by saving the custom keyboard into a static and then binding it to all Entry fields using a default style. You can place that default style into an application-wide resource dictionary, which is automatically applied throughout the application. Here is sample code that I just tested to verify in a new empty Forms project:

    Step 1. Save the custom keyboard into a static.

    Keyboards.cs (static custom keyboard):

    using Xamarin.Forms;
    
    namespace KeyboardDemo
    {
        public static class Keyboards
        {
            public static Keyboard Unassisted { get; private set; }
    
            static Keyboards ()
            {
                Unassisted = Keyboard.Create (0);
            }
        }
    }
    

    Step 2. Create an App.xaml for your project.

    Follow this tip to add an App.xaml to your Forms project: http://jfarrell.net/2015/02/02/centralize-your-styles-with-xamarin-forms/

    Step 3. Add the default style to App.xaml

    App.xaml:

    <?xml version="1.0" encoding="UTF-8"?>
    <Application 
            xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            xmlns:demo="clr-namespace:KeyboardDemo" 
            x:Class="KeyboardDemo.App">
    
        <Application.Resources>
            <ResourceDictionary>
                <Style x:Key="EntryStyle" TargetType="Entry">
                    <Setter Property="Keyboard" Value="{x:Static demo:Keyboards.Unassisted}" />
                </Style>
                <Style BasedOn="{StaticResource EntryStyle}" TargetType="Entry" />
            </ResourceDictionary>
        </Application.Resources>
    
    </Application>
    

    Step 4. Add a new page to the project

    Add a ContentPage to the application, with plain Entry controls to verify the styling.

    App.xaml.cs:

    using Xamarin.Forms;
    
    namespace KeyboardDemo
    {
        public partial class App : Application
        {
            public App ()
            {
                InitializeComponent ();
                MainPage = new MyPage ();
            }
        }
    }
    

    MyPage.xaml:

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage 
            xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            x:Class="KeyboardDemo.MyPage">
        <ContentPage.Padding>
            <OnPlatform x:TypeArguments="Thickness" iOS="0,20,0,0" Android="0" WinPhone="0" />
        </ContentPage.Padding>
    
        <StackLayout>
            <Entry />
        </StackLayout>
    </ContentPage>