Silverlight Password Box






3.50/5 (9投票s)
一篇关于如何在 Silverlight 2 Beta 2 中创建简单密码文本框的小文章

引言
在研究 Silverlight 2 Beta 2 时,我注意到没有可用的密码框。它将在最终版本中提供,但在此之前,我们必须找到另一种方法来实现它。这个解决方案快速,可能有点粗糙,但它有效,这使得它足够有价值可以发布。:)
Using the Code
只需将 tbPassword_TextChanged
方法(来自附件示例)附加到您的密码文本框(简单的 Silverlight 文本框)。
string _currentText = "";
void tbPassword_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox __textBox = sender as TextBox;
if (__textBox != null)
{
string __currentText = __textBox.Text;
if (__currentText.Length < _currentText.Length)
_currentText = _currentText.Substring(0, __currentText.Length);
if (__currentText != "")
{
for (int i = 0; i < __currentText.Length; i++)
{
if (__currentText[i] != '\u25CF')
{
string __temp = __currentText.Remove(i, 1);
__textBox.Text = __temp.Insert(i, "\u25CF");
_currentText = _currentText.Insert
(_currentText.Length, __currentText[i].ToString());
}
}
}
}
}
在 _currentText
中,您将找到密码框中的文本。
关注点
我正在开发一个相当大的 RIA Silverlight 应用程序。缺少这个基本的密码框让我感到不安。希望在 Silverlight 的最终版本中,我们不再遇到这样的问题。
历史
在创建这个小示例时,我受到了 Michael Sync 的帖子 的启发。不幸的是,Silverlight 2 beta 2 示例源代码无法正常工作,因此我决定编写一个更简单的解决方案(恕我直言)。