Как снять маску ввода пароля внутри пользовательского элемента управления в первый клик Xamarin.Form - PullRequest
2 голосов
/ 09 июля 2019

Я создал пользовательский элемент управления, содержащий запись и изображение.

ПРОБЛЕМА

Маска с паролем начинает работать после двух щелчков.

Изначально это так:

enter image description here

Первый щелчок: Изменяет только значок

enter image description here

Второй щелчок: Изменить шрифт и значок enter image description here

После второго щелчка он работает правильно

РЕАЛИЗАЦИЯ

Пользовательский контроль

xml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         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>             
            <Image x:Name="imgFinal" >
                <Image.GestureRecognizers>
                    <TapGestureRecognizer
                     Tapped="ImagemFinal_Tapped"
                     NumberOfTapsRequired="1" />
                </Image.GestureRecognizers>
            </Image>
</AbsoluteLayout>
</ContentView>

Класс:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class EntryControl : ContentView
{
    public bool IsPassword
    {
        get
        {
            return (bool)GetValue(IsPasswordProperty);
        }
        set
        {
            SetValue(IsPasswordProperty, value); 
        }

    }


    public static readonly BindableProperty IsPasswordProperty = BindableProperty.Create(
                                                 propertyName: "IsPassword",
                                                 returnType: typeof(bool),
                                                 declaringType: typeof(EntryControl),
                                                 defaultValue: false,
                                                 defaultBindingMode: BindingMode.TwoWay,
                                                 propertyChanged: IsPasswordPropertyChanged);

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

    private static void IsPasswordPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (EntryControl)bindable;
        if (control == null) return;

        bool isPassword = (bool)newValue;
        control.entry.IsPassword = isPassword;
        control.imgFinal.Source = new FileImageSource { File = isPassword ? imageEyePassaword : imageEyeOffPassaword };
    }

    private void ImagemFinal_Tapped(object sender, EventArgs e)
    {
        IsPassword = !IsPassword; 
    }
    private const string imageEyePassaword = "eye.png";
    private const string imageEyeOffPassaword = "eye_off.png";
}

MainPage

 <control:EntryControl IsPassword="True"></control:EntryControl>

1 Ответ

0 голосов
/ 10 июля 2019

Моя проблема была в <control:CustomEntry x:Name="entry"> </control:CustomEntry>, CustomEntry, это запись, которую я настроил.

 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);                  
            }
        }
    }
}

* 1012 SOLUTION ** * 1013 Я только что удалил эту строку

this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions) 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...