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

查看:111
本文介绍了如何在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专业版中)的Portable lib项目中使用Entry Control:

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

渲染器(Android)

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

GitHub

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

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