XAML 在xamarin中使用带有行为概念的regex进行密码验证

mlmc2os5  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(124)

我需要使用正则表达式验证我的密码。如果条件满足意味着背景颜色将是黑色。如果错误意味着背景颜色显示红色。但对我来说,它总是显示红色,只有无论是否满足。
这是我的密码验证课程:

public class PasswordValidation : Behavior<Entry>
{
   
    protected override void OnAttachedTo(Entry bindable)
    {
        base.OnAttachedTo(bindable);
        bindable.TextChanged += Bindable_TextChanged;
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        base.OnDetachingFrom(bindable);
        bindable.TextChanged -= Bindable_TextChanged;his 
    }

这是XAML页面:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="LoginPage.LoginPage"
             xmlns:behaviors="clr-namespace:LoginPage"
             xmlns:local="clr-namespace:LoginPage"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
   
    <StackLayout Orientation="Vertical" >
    <Image Source="Login1.png" 
           VerticalOptions="Start" 
           HorizontalOptions="Center" />
        <Label 
             
               Text="Login" 
               TextColor="Black" 
               FontSize="35" 
               VerticalOptions="Start" 
               HorizontalOptions="Center" 
               FontAttributes="Bold" 
               />

        <Grid Padding="20" 
              RowSpacing="10" >

            <Label Grid.Row="0" 
                   Grid.ColumnSpan="4" 
                   Text="EMAIL" 
                   TextColor="Black" 
                   FontSize="Medium" 
                   FontAttributes="Bold" />
            
            <Entry  Grid.Row="1" 
                    Grid.ColumnSpan="4"
                    Placeholder="eg:abc@gmail.com" 
                    x:Name="EmailEntry"
                    TextColor="Black"  
                    VerticalOptions="StartAndExpand"
                    >
                <Entry.Behaviors>
                    <behaviors:EmailValidation />
                </Entry.Behaviors>
            </Entry> 
                  
            <Label  Grid.Row="2" 
                    Grid.ColumnSpan="4" 
                    Text="PASSWORD" 
                    TextColor="Black" 
                    FontSize="Medium" 
                    FontAttributes="Bold" />

            <Entry Grid.Row="3" Grid.ColumnSpan="4" 
                   Placeholder="Enter Password" 
                   x:Name="PasswordEntry"
                   
                   IsPassword="{Binding Source={x:Reference ShowPasswordActionTrigger},
                   Path=HidePassword}" >
                <Entry.Behaviors>
                    <behaviors:PasswordValidation />
                </Entry.Behaviors>
                
            </Entry>

            <ImageButton Grid.Row="3" 
                         Grid.Column="4" 
                         VerticalOptions="Center" 
                         Margin="0,0,10,0"
                         HeightRequest="35"
                         HorizontalOptions="End"
                         Source="eyeoff.png">
                
                <ImageButton.Triggers>
                    <EventTrigger Event="Clicked">
                        <local:ShowPasswordTrigger ShowIcon="eyeon"
                                                   HideIcon="eyeoff"
                                                   x:Name="ShowPasswordActionTrigger" />
                        </EventTrigger>
                </ImageButton.Triggers>
            </ImageButton>
            
           

    </Grid>

        <Button   
                 Text="Login" VerticalOptions="StartAndExpand" 
                     HorizontalOptions="Center" Clicked="Button_Clicked"/>
    </StackLayout>

</ContentPage>

请帮助我解决这个错误。

hyrbngr7

hyrbngr71#

您可以尝试以下代码:

public class PasswordValidation: Behavior<Entry> 
{
    void Bindable_TextChanged(object sender, TextChangedEventArgs args)
    {

        bool isValid = args.NewTextValue.Trim().Equals("123456");

        ((Entry)sender).BackgroundColor = isValid ? Color.Black : Color.Red;
    }

    protected override void OnAttachedTo(Entry bindable)
    {
        base.OnAttachedTo(bindable);
        bindable.TextChanged += Bindable_TextChanged;
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        base.OnDetachingFrom(bindable);
        bindable.TextChanged -= Bindable_TextChanged; 
    }
}

备注:

Bindable_TextChanged方法中,我简单的使用了代码bool isValid = args.NewTextValue.Trim().Equals("123456");作为条件,你可以根据自己的需要改变它。

相关问题