在Xamarin表单中屏蔽密码字符无效

在Xamarin表单中屏蔽密码字符无效,xamarin,xamarin.forms,xamarin.android,xamarin.ios,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我想部分屏蔽密码字段,从点到星号。我试过用转换器,但没用。以xamarin的形式实现这一点的最佳方式是什么 <Entry IsPassword="True" Placeholder="password" Text="{Binding Password.Value, Mode=TwoWay, Converter={StaticResource MaskedPasswordConverter}}"

我想部分屏蔽密码字段,从点到星号。我试过用转换器,但没用。以xamarin的形式实现这一点的最佳方式是什么

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

公共类掩码密码转换器:IValueConverter
{
私有字符串_值;
公共对象转换(对象值、类型targetType、对象参数、CultureInfo
(文化)
{
var str=(值??).ToString();
_值=str;
var maskedStr=“”;
如果(!string.IsNullOrEmpty(str)&&str.Length>2)
{
var domainStr=str.IndexOf('@');
var lengthOfMask=domainStr-2;
maskedStr=str.Substring(0,2)+新字符串('*',lengthOfMask)+
str.Substring(domainStr);
}
返回遮罩;
}
公共对象转换回(对象值、类型targetType、对象参数、,
文化信息(文化)
{
返回值;
}
}

我建议您为此使用行为。 您可以了解更多关于Xamarin表单行为的信息

更多例子


希望这有帮助。

如果您想使用IValueConverter使用星号屏蔽部分密码,我认为您可以将绑定模式设置为单向,然后请确认您的密码中有@字符

我建议你可以用这种方式屏蔽电子邮件,不要屏蔽密码,但你仍然想这样做,这是你可以看的示例:

 <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));
        }
    }
}
这是屏幕截图:

但我仍然建议您可以使用自定义渲染来使用星号屏蔽密码,这是关于此的示例,您可以查看:

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

如果尝试使用IsPassword=“False”,则需要使用自定义渲染器?你在做你自己的mask@AlexanderRojas它仍然不起作用