如何更改 Xamarin 表单中的密码屏蔽字符 - 条目 [英] How to change password masking character in Xamarin forms - Entry

查看:21
本文介绍了如何更改 Xamarin 表单中的密码屏蔽字符 - 条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到了一个相当简单的问题,最终让我陷入了死胡同.我正在构建一个使用 Xamarin Forms 的应用程序,并希望在用户将密码从项目符号输入为星号时更改掩码字符.

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.

为了输入密码,我在我的内容页面(在 VS2017 专业版中)使用便携式库项目中的输入控件:

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" />

我知道我可能应该在 Android 项目中为此创建一个自定义渲染器,但我真的很感激如何为这个特定目的而做.

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.

推荐答案

我确信转换器的答案会起作用,但作为个人偏好,我不喜欢它.对我来说,这看起来像是渲染器的工作.

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

这是我将如何做到的(仅在 android 中举例,因为我没有 ios,但在那里实现它相当简单)

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)

使用 Xamarin 表单

Usage Xamarin forms

        <controls:PasswordBox Placeholder="Password"/>

渲染器(安卓)

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

GitHub

这篇关于如何更改 Xamarin 表单中的密码屏蔽字符 - 条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆