Xamarin.forms Xamarin表单:隐藏和显示密码功能不适用于输入
我已经为entry组件实现了隐藏和显示密码功能。它最初不工作,但在一次隐藏和显示之后,它开始工作。图标正在更改,但密码未显示。此问题仅在android平台上存在 我的代码: Xaml:Xamarin.forms Xamarin表单:隐藏和显示密码功能不适用于输入,xamarin.forms,Xamarin.forms,我已经为entry组件实现了隐藏和显示密码功能。它最初不工作,但在一次隐藏和显示之后,它开始工作。图标正在更改,但密码未显示。此问题仅在android平台上存在 我的代码: Xaml: <StackLayout Padding="3" Orientation="Horizontal"> <local:CustomEntry x:Name="password_entry" IsP
<StackLayout Padding="3" Orientation="Horizontal">
<local:CustomEntry
x:Name="password_entry"
IsPassword="True"
Placeholder="Password"
Style="{StaticResource LoginEntryStyle}"/>
<Image
x:Name="password_icon"
Style="{StaticResource LoginEntryImageStyle}"
HorizontalOptions="End"
Source="ic_hide_password_xx.png">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="PasswordIcon_Clicked"
NumberOfTapsRequired="1">
</TapGestureRecognizer>
</Image.GestureRecognizers>
</Image>
</StackLayout>
bool showPassword = true;
private void PasswordIcon_Clicked(object sender, EventArgs e)
{
if (showPassword)
{
password_icon.Source = "ic_show_password_xx.png";
password_entry.IsPassword = false;
showPassword = false;
}
else
{
password_icon.Source = "ic_hide_password_xx.png";
password_entry.IsPassword = true;
showPassword = true;
}
}
我没有使用bool变量,而是尝试使用
password\u entry.IsPassword
,但没有成功。我上传了一个示例项目供参考。这似乎是一个潜在问题,您可以在github上提出
变通办法
您可以使用触发器轻松地执行此操作,并仅检查条目的
IsPassword
属性
<StackLayout Padding="3" Orientation="Horizontal">
<local:CustomEntry
x:Name="password_entry"
IsPassword="True"
Placeholder="Password"
Style="{StaticResource LoginEntryStyle}"/>
<Image
Style="{StaticResource LoginEntryImageStyle}"
HorizontalOptions="End">
<Image.Triggers>
<DataTrigger
TargetType="Image"
Binding="{Binding Source={x:Reference password_entry}, Path=IsPassword}"
Value="True">
<Setter
Property="Source"
Value="ic_show_password_xx" />
</DataTrigger>
<DataTrigger
TargetType="Image"
Binding="{Binding Source={x:Reference password_entry}, Path=IsPassword}"
Value="False">
<Setter
Property="Source"
Value="ic_hide_password_xx" />
</DataTrigger>
</Image.Triggers>
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="PasswordIcon_Clicked" />
</Image.GestureRecognizers>
</Image>
</StackLayout>
为什么要将
showPassword
bool初始化为true?@FabriBertani用于处理初始点击
<StackLayout Padding="3" Orientation="Horizontal">
<local:CustomEntry
x:Name="password_entry"
IsPassword="True"
Placeholder="Password"
Style="{StaticResource LoginEntryStyle}"/>
<Image
Style="{StaticResource LoginEntryImageStyle}"
HorizontalOptions="End">
<Image.Triggers>
<DataTrigger
TargetType="Image"
Binding="{Binding Source={x:Reference password_entry}, Path=IsPassword}"
Value="True">
<Setter
Property="Source"
Value="ic_show_password_xx" />
</DataTrigger>
<DataTrigger
TargetType="Image"
Binding="{Binding Source={x:Reference password_entry}, Path=IsPassword}"
Value="False">
<Setter
Property="Source"
Value="ic_hide_password_xx" />
</DataTrigger>
</Image.Triggers>
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="PasswordIcon_Clicked" />
</Image.GestureRecognizers>
</Image>
</StackLayout>
private void PasswordIcon_Clicked(object sender, EventArgs e)
{
password_entry.IsPassword = !password_entry.IsPassword;
}