在 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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM