How to change password masking character in Xamari

2019-08-21 00:52发布

I've currently faced a rather simple issue which eventually put me to a dead end. I am building an application that uses Xamarin Forms and want to change a masking character when user enters password from a bullet to an asterisk.

For entering password I'm using Entry control in Portable lib project in my content page (in VS2017 professional):

<Entry x:Name="Entry_Password" Placeholder="Password" IsPassword="True" />

I know that I probably should create a custom Renderer in Android project for this one, but would really appreciate how to do it for this specific purpose.

2条回答
该账号已被封号
2楼-- · 2019-08-21 01:08

Am sure the converter answer will work , but as a personal preference i dont like it. this looks like a renderer job to me .

and here is how i would do it(example only in android because i dont have ios, but its fairly simple to implement it there)

Usage Xamarin forms

        <controls:PasswordBox Placeholder="Password"/>

The Renderer (Android)

[assembly: ExportRenderer(typeof(PasswordBox), typeof(PasswordBoxRenderer))]
namespace PasswordAsterisk.Droid.Renderers
{
public class PasswordBoxRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.InputType = Android.Text.InputTypes.TextVariationPassword |
                      Android.Text.InputTypes.ClassText;

            Control.TransformationMethod = new HiddenPasswordTransformationMethod();
        }
    }
}
internal class HiddenPasswordTransformationMethod : Android.Text.Method.PasswordTransformationMethod
{
    public override Java.Lang.ICharSequence GetTransformationFormatted(Java.Lang.ICharSequence source, Android.Views.View view)
    {
        return new PasswordCharSequence(source);
    }
}

internal class PasswordCharSequence : Java.Lang.Object, Java.Lang.ICharSequence
{
    private Java.Lang.ICharSequence _source;
    public PasswordCharSequence(Java.Lang.ICharSequence source)
    {
        _source = source;
    }

    public char CharAt(int index)
    {
        return '*';
    }

    public int Length()
    {
        return _source.Length();
    }

    public Java.Lang.ICharSequence SubSequenceFormatted(int start, int end)
    {
        return _source.SubSequenceFormatted(start, end); 
    }

    public IEnumerator<char> GetEnumerator()
    {
        return _source.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return _source.GetEnumerator();
    }
}
}

full source code example in GitHub

查看更多
别忘想泡老子
3楼-- · 2019-08-21 01:10

An easyier way to do this would be to use a Converter to swap every letter in an asterisk and than when you request the value it is plain.

This is an interesting post that could help you with your problem: https://forums.xamarin.com/discussion/52354/is-there-a-way-to-partially-mask-an-entry-field-in-xamarin

It's imported to use oneway and not twoway!

Good luck

查看更多
登录 后发表回答