masking password character in Xamarin forms is not working

Viewed 637

I would like to partially mask the password field from dots to asterisk. I tried using a converter but it doesn't work. What is the best way to achieve this in xamarin forms.

     <Entry IsPassword="True"
            Placeholder="password"
            Text="{Binding Password.Value, Mode=TwoWay, Converter={StaticResource 
            MaskedPasswordConverter}}"
            MaxLength="6">

     public class MaskedPasswordConverter : IValueConverter
     {

       private string _value;
       public object Convert(object value, Type targetType, object parameter, CultureInfo 
       culture)
       {
         var str = (value ?? "").ToString();
         _value = str;
         var maskedStr = "";
         if (!string.IsNullOrEmpty(str) && str.Length > 2)
         {
            var domainStr = str.IndexOf('@');
            var lengthOfMask = domainStr - 2;

            maskedStr = str.Substring(0, 2) + new string('*', lengthOfMask) + 
            str.Substring(domainStr);
         }
         return maskedStr;
       }

       public object ConvertBack(object value, Type targetType, object parameter, 
       CultureInfo culture)
       {
         return value;
       }
    }
2 Answers

I suggest you use behaviors for this. You can find out more about Xamarin forms behaviors here

More examples here

Hope this helps.

If you want to use IValueConverter to mask partial password using asterisk, I think you can set binding mode as OneWay, then please confirm that there is @ character in your Password.

I suggest you can use this way to mask email, don't mask password, but you still want to do ,this is the sample that you can take a look:

 <Entry
            MaxLength="6"
            Placeholder="password"
            Text="{Binding password, Mode=OneWay, Converter={StaticResource converter1}}" />

public partial class Page24 : ContentPage, INotifyPropertyChanged
{
    private string _password;
    public string password
    {
        get
        { return _password; }
        set
        {
            _password = value;
            RaisePropertyChanged("password");               
        }
    }
    public Page24()
    {
        InitializeComponent();

        password = "123@56";
        this.BindingContext = this;
    }


    public event PropertyChangedEventHandler PropertyChanged;      
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

IValueConverter:

public class Passwordconverter : IValueConverter
{      
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var str = (value ?? "").ToString();

        var maskedStr = "";
        if (!string.IsNullOrEmpty(str) && str.Length > 2)
        {
            var domainStr = str.IndexOf('@');
            var lengthOfMask = domainStr - 2;

            maskedStr = str.Substring(0, 2) + new string('*', lengthOfMask) + str.Substring(domainStr);
        }
        return maskedStr;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

This is the screenshot:

enter image description here

But I still suggest you can use custom render to mask your password using asterisk, this is the sample about this, you can take a look:

How to change password masking character in Xamarin forms - Entry

Related