What is the Error in This Code? , I Have Exception :
I Try to make validation using behaviors , try this before the logic code right
<local:BooleanToObjectConverter x:Key="boolToStyleImage" x:TypeArguments="Style">
<local:BooleanToObjectConverter.FalseObject>
<Style TargetType="Image">
<Setter Property="HeightRequest" Value="20" />
<Setter Property="Source" Value="{local:ImageResource MVVM_Demo_And_Service.Images.error.png}" />
</Style>
</local:BooleanToObjectConverter.FalseObject>
<local:BooleanToObjectConverter.TrueObject>
<Style TargetType="Image">
<Setter Property="HeightRequest" Value="20" />
<Setter Property="Source" Value="{local:ImageResource MVVM_Demo_And_Service.Images.success.png}" />
</Style>
</local:BooleanToObjectConverter.TrueObject>
</local:BooleanToObjectConverter>
<Style x:Key="baseStyle"
TargetType="Label">
<Setter Property="FontSize" Value="Micro" />
<Setter Property="FontAttributes" Value="Italic" />
</Style>
<local:BooleanToObjectConverter x:Key="boolToStyleName"
x:TypeArguments="Style">
<local:BooleanToObjectConverter.FalseObject>
<Style TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="TextColor" Value="#F44336" />
<Setter Property="Text" Value="Enter your Name " />
</Style>
</local:BooleanToObjectConverter.FalseObject>
<local:BooleanToObjectConverter.TrueObject>
<Style TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="TextColor" Value="#4CAF50" />
<Setter Property="Text" Value="Good" />
</Style>
</local:BooleanToObjectConverter.TrueObject>
</local:BooleanToObjectConverter>
<local:BooleanToObjectConverter x:Key="boolToStyleEmail"
x:TypeArguments="Style">
<local:BooleanToObjectConverter.FalseObject>
<Style TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="TextColor" Value="#F44336" />
<Setter Property="Text" Value="Enter a valid email" />
</Style>
</local:BooleanToObjectConverter.FalseObject>
<local:BooleanToObjectConverter.TrueObject>
<Style TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="TextColor" Value="#4CAF50" />
<Setter Property="Text" Value="Your email looks good" />
</Style>
</local:BooleanToObjectConverter.TrueObject>
</local:BooleanToObjectConverter>
<local:BooleanToObjectConverter x:Key="boolToStyleAge"
x:TypeArguments="Style">
<local:BooleanToObjectConverter.FalseObject>
<Style TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="TextColor" Value="#F44336" />
<Setter Property="Text" Value="Enter numeric values" />
</Style>
</local:BooleanToObjectConverter.FalseObject>
<local:BooleanToObjectConverter.TrueObject>
<Style TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="TextColor" Value="#4CAF50" />
<Setter Property="Text" Value="You are very young!" />
</Style>
</local:BooleanToObjectConverter.TrueObject>
</local:BooleanToObjectConverter>
<local:BooleanToObjectConverter x:Key="boolToStylePassword"
x:TypeArguments="Style">
<local:BooleanToObjectConverter.FalseObject>
<Style TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="TextColor" Value="#F44336" />
<Setter Property="Text" Value="Enter numeric values" />
</Style>
</local:BooleanToObjectConverter.FalseObject>
<local:BooleanToObjectConverter.TrueObject>
<Style TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="TextColor" Value="#4CAF50" />
<Setter Property="Text" Value="You are very Complex!" />
</Style>
</local:BooleanToObjectConverter.TrueObject>
</local:BooleanToObjectConverter>
<local:GenderConverter x:Key="genderConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<Entry Grid.Row="0" Grid.Column="1" Placeholder="UserName"
Text="{Binding SelectedUser.UserName, Mode=TwoWay}"
x:Name="NameEntry" >
<Entry.Behaviors>
<local:MaxLengthValidator MaxLength="10"/>
</Entry.Behaviors>
</Entry>
<Image Grid.Row="0" Grid.Column="2" x:Name="NameSucessErrorImage"
Style="{Binding Source={x:Reference nameValidator},
Path=IsValid,
Converter={StaticResource boolToStyleImage}}"/>
<Label Grid.Row="1" Grid.Column="1" Style="{Binding Source={x:Reference nameValidator},
Path=IsValid,
Converter={StaticResource boolToStyleName}}" />
<Entry Grid.Row="2" Grid.Column="1" Placeholder="Password"
Text="{Binding SelectedUser.UserPassword, Mode=TwoWay}"
x:Name="PassEntry" >
<Entry.Behaviors>
<local:NumberValidatorBehavior x:Name="passwordValidator"/>
<local:MaxLengthValidator MaxLength="15"/>
</Entry.Behaviors>
</Entry>
<Image Grid.Row="2" Grid.Column="2" x:Name="passSuccessErrorImage"
Style="{Binding Source={x:Reference passwordValidator},
Path=IsValid,
Converter={StaticResource boolToStyleImage}}"/>
<Label Grid.Row="3" Grid.Column="1" Style="{Binding Source={x:Reference passwordValidator},
Path=IsValid,
Converter={StaticResource boolToStylePassword}}" />
<Entry Grid.Row="3" Grid.Column="1" Text="{Binding SelectedUser.UserAge, Mode=TwoWay}"
x:Name="AgeEntry" >
<Entry.Behaviors>
<local:NumberValidatorBehavior x:Name="ageValidator"/>
<local:MaxLengthValidator MaxLength="2"/>
</Entry.Behaviors>
</Entry>
<Image Grid.Row="3" Grid.Column="2" x:Name="ageSuccessErrorImage"
Style="{Binding Source={x:Reference ageValidator},
Path=IsValid,
Converter={StaticResource boolToStyleImage}}"/>
<Label Grid.Row="4" Grid.Column="1" Style="{Binding Source={x:Reference ageValidator},
Path=IsValid,
Converter={StaticResource boolToStyleAge}}" />
</Grid>
<Button Text="ADD User"
Command="{Binding PostCommand}" Clicked="Button_Clicked" />
</StackLayout>
Any help ?
I Solved it , the error was in :
<Entry Grid.Row="0" Grid.Column="1" Placeholder="UserName"
Text="{Binding SelectedUser.UserName, Mode=TwoWay}"
x:Name="NameEntry" >
<Entry.Behaviors>
<local:MaxLengthValidator MaxLength="10" x:Name="nameValidator"/>
</Entry.Behaviors>
</Entry>
<Image Grid.Row="0" Grid.Column="2" x:Name="NameSucessErrorImage"
Style="{Binding Source={x:Reference nameValidator},
Path=IsValid,
Converter={StaticResource boolToStyleImage}}"/>
<Label Grid.Row="1" Grid.Column="1" Style="{Binding Source={x:Reference nameValidator},
Path=IsValid,
Converter={StaticResource boolToStyleName}}" />
I didn't define x:Name="nameValidator" at local:MaxLengthValidator tag to reference to Behavior in image and label tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With