首页 > 解决方案 > Xamarin - 将 textchange 和 unfocus 事件分配给文本框(密码)

问题描述

我的 XAML 文件看起来像这样

   <local:PlainEntry IsPassword="True" Placeholder="Pwd"
    x:Name="contrasenaEntry" HorizontalOptions="Fill" 
    VerticalOptions="Center"  Text="{Binding Password, Mode=TwoWay}" Margin="0"/>

有没有办法在我尝试使用 Password.myElement.IsFocused 的文本字段 Password 上触发 Onchange 和 unfocus 事件,但它不起作用。我试过了

private void PasswordEditText_TextChanged(object sender, TextChangedEventArgs e)
{
    Console.WriteLine("text has changed!");
    // this is not working!
}

标签: xamlxamarin

解决方案


它显示您正在绑定到属性“密码”,这看起来像是您正在使用 MVVM,但是您编写的那个方法看起来像是一个代码隐藏方法。

如果你在后面使用代码,你会想要使用这样的东西..

<local:PlainEntry IsPassword="True" Placeholder="Pwd"
x:Name="contrasenaEntry" HorizontalOptions="Fill" 
VerticalOptions="Center" TextChanged="PasswordEditText_TextChanged" 
Margin="0"/>

然后调用你已有的方法

private void PasswordEditText_TextChanged(object sender, TextChangedEventArgs e)
{
    Console.WriteLine("text has changed!");
    // this is not working!
}

推荐阅读