How change Keyboard of entry inside user control Xamarin Fom

JoyPeterJoyPeter Member ✭✭
edited July 9 in Xamarin.Forms

I can not change the entry keyboard inside user conttrol

IMPLEMENTATION

User control

xml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView
         xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
         xmlns:control="clr-namespace:Projecro_3.ControlCostumiado"
         prism:ViewModelLocator.AutowireViewModel="True"     
         Padding="10"
         x:Class="Projecro_3.Controls.EntryControl">

 <AbsoluteLayout>

           <control:CustomEntry x:Name="entry">  </control:CustomEntry>             
</AbsoluteLayout>
</ContentView>

Classe:

namespace Projecro_3.Controls
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class EntryControl : ContentView, INotifyPropertyChanged
    {

        public Keyboard Teclado { get; set; }
        public static readonly BindableProperty TecladoProperty = BindableProperty.Create(
                                                   propertyName: "Teclado",
                                                   returnType: typeof(Keyboard),
                                                   declaringType: typeof(EntryControl),
                                                   defaultValue: Keyboard.Default,
                                                   defaultBindingMode: BindingMode.TwoWay,
                                                   propertyChanged: TecladoPropertyChanged);


        public EntryControl()
        {
            InitializeComponent(); 
            entry.BindingContext = this;
        }

        private static void TecladoPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (EntryControl)bindable;
            if (control == null) return;
            control.entry.Keyboard = (Keyboard)newValue;
        }  
    }
}

MainPage

<control:EntryControl Grid.Row="0" Teclado="Numeric" WidthRequest="180" PathIconeInicial="lock.png" Titulo="Alegria" Placeholder="Number" ></control:EntryControl>

I set the numeric keyboard, it prevents typing texts but does not change keyboard.

Tagged:

Best Answer

  • JoyPeterJoyPeter ✭✭
    Accepted Answer

    I solved the problem!

    My problem was in the <control:CustomEntry x:Name="entry"> </control:CustomEntry> , CustomEntry it's an entry that I've customized.

    public class CustomEntry : Entry
     { 
    
     }
    

    Android

    [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
    namespace Projecro_3.Droid
    {
        public class CustomEntryRenderer : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    GradientDrawable gd = new GradientDrawable();
                    gd.SetColor(global::Android.Graphics.Color.Transparent);
                    this.Control.SetBackgroundDrawable(gd);
                    this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions);                  
                }
            }
        }
    }
    

    SOLUTION

    I just removed this line

    this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions)

Answers

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited July 9
    public Keyboard Teclado
    {
        get => (Keyboard)GetValue(TecladoProperty);
        set => SetValue(TecladoProperty, value);
    }
    

    instead of just get; set;

  • JoyPeterJoyPeter Member ✭✭
    Accepted Answer

    I solved the problem!

    My problem was in the <control:CustomEntry x:Name="entry"> </control:CustomEntry> , CustomEntry it's an entry that I've customized.

    public class CustomEntry : Entry
     { 
    
     }
    

    Android

    [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
    namespace Projecro_3.Droid
    {
        public class CustomEntryRenderer : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    GradientDrawable gd = new GradientDrawable();
                    gd.SetColor(global::Android.Graphics.Color.Transparent);
                    this.Control.SetBackgroundDrawable(gd);
                    this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions);                  
                }
            }
        }
    }
    

    SOLUTION

    I just removed this line

    this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions)

  • JoyPeterJoyPeter Member ✭✭

    @Amar_Bait said:

    public Keyboard Teclado
    {
      get => (Keyboard)GetValue(TecladoProperty);
      set => SetValue(TecladoProperty, value);
    }
    

    instead of just get; set;

    Thank you very much :)

Sign In or Register to comment.