Xamarin.Forms 创建移动应用程序的基础知识 5 - Entry

简介

演示 Entry 属性 。

  1. 在 XAML 中创建 Xamarin.Forms Entry 。
  2. 响应 Entry 的文本更改。
  3. Entry 属性简介。

创建 Entry

  1. 打开已有项目 AwesomeApp。
  2. 添加新项 EntryPage.xaml。
  3. 编辑 EntryPage.xaml:
<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="AwesomeApp.EntryPage">
	<ContentPage.Content>
		<StackLayout Margin="20,32">
			<Entry Placeholder="输入文本" />
		</StackLayout>
	</ContentPage.Content>
</ContentPage>
  1. 编辑 App.xaml.cs:
public App()
{
	InitializeComponent();
	MainPage = new EntryPage();
}
  1. 调试 Android 上的应用:

响应 Entry 的文本更改

  1. 编辑 EntryPage.xaml:
<StackLayout Margin="20,32">
	<Entry Placeholder="输入文本"
		   TextChanged="OnEntryTextChanged"
		   Completed="OnEntryCompleted" />
	<Label Text="OldText"
		   TextColor="LightGray"/>
	<Label x:Name="labOld" />
	<Label Text="NewText"
		   TextColor="LightGray" />
	<Label x:Name="labNew" />
	<Label Text="CompletedText"
		   TextColor="LightGray" />
	<Label x:Name="labCompleted" />
</StackLayout>

*使用返回或完成键完成 Entry 中的文本输入时,才会触发 Completed 事件

  1. 编辑 EntryPage.xaml.cs:
public void OnEntryTextChanged(object sender, TextChangedEventArgs e)
{
	labOld.Text = e.OldTextValue;
	labNew.Text = e.NewTextValue;
}

public void OnEntryCompleted(object sender, EventArgs e)
{
	labCompleted.Text = ((Entry)sender).Text;
}
  1. 调试 Android 上的应用:

Entry 属性简介

  1. 编辑 EntryPage.xaml:
<Entry Placeholder="输入文本"
	   MaxLength="15"
	   IsSpellCheckEnabled="false"
	   IsTextPredictionEnabled="false"
	   IsPassword="true"
	   TextChanged="OnEntryTextChanged"
	   Completed="OnEntryCompleted" />
  • MaxLength 限制输入长度
  • IsSpellCheckEnabled 禁用拼写检查
  • IsTextPredictionEnabled 禁用文本预测和自动文本预测
  • IsPassword 对输入的字符进行掩码
  1. 调试 Android 上的应用: