Last active
April 14, 2017 06:02
-
-
Save rc1021/2e9a69fdb91d545f5e2ad6e4aa0a35f6 to your computer and use it in GitHub Desktop.
加入PasswordEntryCell,讓 Xamarin 具有密碼輸入的 EntryCell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 這檔案是參考 https://forums.xamarin.com/discussion/21298/entrycell-password 並加以修正。 | |
* 主要是 xamarin 目前暫無密碼輸入的 EntryCell, | |
* | |
* 我增加的部份: | |
* 1. 建立 BindableProperty 讓 XAML 也可以使用 Value 屬性 | 2017.04 | |
*/ | |
using System; | |
using Xamarin.Forms; | |
namespace App1 | |
{ | |
public class PasswordEntryCell : EntryCell | |
{ | |
public static readonly BindableProperty ValueProperty = BindableProperty.Create("Value", typeof(string), typeof(PasswordEntryCell), null, BindingMode.TwoWay); | |
public string Value | |
{ | |
get { return (string)GetValue(ValueProperty); } | |
set | |
{ | |
SetValue(ValueProperty, value); | |
setStars(); | |
} | |
} | |
private string starFiller(int count) | |
{ | |
var output = ""; | |
for (; count > 0; count--, output += "●") | |
; | |
return output; | |
} | |
private void setStars() | |
{ | |
this.Text = starFiller(this.Value.Length); | |
} | |
public PasswordEntryCell() | |
{ | |
Value = ""; | |
this.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) => | |
{ | |
if (e.PropertyName == "Text") | |
{ | |
var txtVal = ((EntryCell)sender).Text; | |
var txtLen = (txtVal == null) ? 0 : txtVal.Length; | |
var mdlVal = ((PasswordEntryCell)sender).Value; | |
var mdlLen = (mdlVal == null) ? 0 : mdlVal.Length; | |
if (txtLen > mdlLen) | |
{ | |
((PasswordEntryCell)sender).Value += txtVal.Substring(txtLen - 1); | |
} | |
else | |
{ | |
((PasswordEntryCell)sender).Value = ((PasswordEntryCell)sender).Value.Substring(0, txtLen); | |
} | |
setStars(); | |
} | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment