在 Xamarin.Forms 实现密码输入EntryCell

  在 Xamarin.Forms 中,我们通常使用 TableView 来构建输入表单。Xamarin 为我们提供了 EntryCell 用于输入文本,但是其并不支持密码输入,即密码掩码。这里要对 EntryCell 进行扩展,使其支持密码输入。

  首先,我们需要在共享项目(多平台项目)中增加控件定义,我们称之为:ExtendedEntryCell

 1     /// <summary>
 2     /// An extended entry cell control that allows set IsPassword
 3     /// </summary>
 4     public class ExtendedEntryCell : EntryCell
 5     {
 6 
 7         /// <summary>
 8         /// The IsPassword property
 9         /// </summary>
10         public static readonly BindableProperty IsPasswordProperty = BindableProperty.Create<ExtendedEntryCell, bool>(p => p.IsPassword, false);
11 
12         /// <summary>
13         /// Gets or sets IsPassword 
14         /// </summary>
15         public bool IsPassword
16         {
17             get { return (bool)GetValue(IsPasswordProperty); }
18             set { SetValue(IsPasswordProperty, value); }
19         }
20     }

我们需要在Android和iOS中加入各自的Render。

Android 平台的 Render:

 1 using Android.Content;
 2 using Android.Text.Method;
 3 using Android.Views;
 4 using Android.Widget;
 5 using WsLink.App.Controls;
 6 using Xamarin.Forms;
 7 using Xamarin.Forms.Platform.Android;
 8 using View = Android.Views.View;
 9 
10 [assembly: ExportRenderer(typeof(ExtendedEntryCell), typeof(ExtendedEntryCellRenderer))]
11 
12 namespace App.Controls
13 {
14     public class ExtendedEntryCellRenderer : EntryCellRenderer
15     {
16         protected override View GetCellCore(Cell item, View convertView, ViewGroup parent, Context context)
17         {
18             var cell = base.GetCellCore(item, convertView, parent, context);
19             var textField = (cell as EntryCellView)?.EditText as TextView;
20 
21             if (textField != null && textField.TransformationMethod != PasswordTransformationMethod.Instance)
22             {
23                 textField.TransformationMethod = PasswordTransformationMethod.Instance;
24             }
25             return cell;
26         }
27     }
28 }

iOS 平台的 Render 

 1 using UIKit;
 2 using WsLink.App.Controls;
 3 using Xamarin.Forms;
 4 using Xamarin.Forms.Platform.iOS;
 5 
 6 [assembly: ExportRenderer(typeof(ExtendedEntryCell), typeof(ExtendedEntryCellRenderer))]
 7 
 8 namespace WsLink.App.Controls
 9 {
10     public class ExtendedEntryCellRenderer : EntryCellRenderer
11     {
12         public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
13         {
14             var entryCell = (ExtendedEntryCell) item;
15             var cell = base.GetCell(item, reusableCell, tv);
16             if (cell != null)
17             {
18                 var textField = (UITextField) cell.ContentView.Subviews[0];
19                 textField.SecureTextEntry = entryCell.IsPassword;
20             }
21             return cell;
22         }
23     }
24 }

使用方法

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:WsLink.App.Controls"
             x:Class="App.Account.LoginPage" Title="登录">
  <TableView Intent="Form">
    <TableSection>
      <EntryCell Label="账户" Placeholder="账户" Text="{Binding Username}"></EntryCell>
      <controls:ExtendedEntryCell IsPassword="True" Label="密码" Placeholder="密码" Text="{Binding Password}"></controls:ExtendedEntryCell>
    </TableSection>
  </TableView>
</ContentPage>

 

posted @ 2016-08-29 14:48  Soar、毅  阅读(3405)  评论(1编辑  收藏  举报