当输入框内的值实时大于8时(xamarin.forms)在输入框下方显示一个标签
Make a label appear under the entry box when the value in the entry box is > 8 in real time (xamarin.forms)
我读过 triggers/behaviors,它似乎只更改了当前的输入框属性。
但就我而言,如果实时值 >8,我想在我的输入框下方显示一个标签。
对于我的其他验证,我正在使用 Fluent Validation,当用户单击保存按钮时验证完成,但这不是我想要做的,因为 >8 只是一个警告,可以保存。
所以我必须找到一种方法来在用户在输入框中输入大于 8 的数字时立即显示警告。
在 xamarin.forms 中有没有办法做到这一点?另外,也许有一种方法可以使用 FluentValidation 来做到这一点,但不确定。
谢谢。
编辑试图用 TextChanged 实现
xaml
<control:MaskedEntry Placeholder="HH:MM:SS" Mask="XX:XX:XX" Keyboard="Numeric" Text="{Binding TaskDuration}" TextChanged="DurationIs8"></control:MaskedEntry>
<Label x:Name="errorMessage" Text="Greater than 8" IsVisible="False" ></Label>
xaml.cs
private void DurationIs8(object sender, TextChangedEventArgs e)
{
var entryText = ((Entry)sender).Text;
var value = Helper.GetDuration(entryText);
if(value.TotalHours > 8)
{
errorMessage.IsVisible = true;
}
}
将输入框转换为小时的GetDuration方法
public static TimeSpan GetDuration(string duration)
{
var value = duration.Split(':').Select(int.Parse).ToArray();
var datetime = new TimeSpan(value[0], value[1], value[2]);
return datetime;
}
private void DurationIs8(object sender, TextChangedEventArgs e)
{
var entryText = ((Entry)sender).Text;
if (entryText == null || entryText.Length < 8) return;
var value = Helper.GetDuration(entryText);
if(value != null && value.TotalHours > 8)
{
errorMessage.IsVisible = true;
}
}
public static TimeSpan GetDuration(string duration)
{
var value = duration.Split(':').Select(int.Parse).ToArray();
if (value.Length < 3) return TimeSpan.Zero;
var datetime = new TimeSpan(value[0], value[1], value[2]);
return datetime;
}
我读过 triggers/behaviors,它似乎只更改了当前的输入框属性。
但就我而言,如果实时值 >8,我想在我的输入框下方显示一个标签。
对于我的其他验证,我正在使用 Fluent Validation,当用户单击保存按钮时验证完成,但这不是我想要做的,因为 >8 只是一个警告,可以保存。
所以我必须找到一种方法来在用户在输入框中输入大于 8 的数字时立即显示警告。
在 xamarin.forms 中有没有办法做到这一点?另外,也许有一种方法可以使用 FluentValidation 来做到这一点,但不确定。
谢谢。
编辑试图用 TextChanged 实现
xaml
<control:MaskedEntry Placeholder="HH:MM:SS" Mask="XX:XX:XX" Keyboard="Numeric" Text="{Binding TaskDuration}" TextChanged="DurationIs8"></control:MaskedEntry>
<Label x:Name="errorMessage" Text="Greater than 8" IsVisible="False" ></Label>
xaml.cs
private void DurationIs8(object sender, TextChangedEventArgs e)
{
var entryText = ((Entry)sender).Text;
var value = Helper.GetDuration(entryText);
if(value.TotalHours > 8)
{
errorMessage.IsVisible = true;
}
}
将输入框转换为小时的GetDuration方法
public static TimeSpan GetDuration(string duration)
{
var value = duration.Split(':').Select(int.Parse).ToArray();
var datetime = new TimeSpan(value[0], value[1], value[2]);
return datetime;
}
private void DurationIs8(object sender, TextChangedEventArgs e)
{
var entryText = ((Entry)sender).Text;
if (entryText == null || entryText.Length < 8) return;
var value = Helper.GetDuration(entryText);
if(value != null && value.TotalHours > 8)
{
errorMessage.IsVisible = true;
}
}
public static TimeSpan GetDuration(string duration)
{
var value = duration.Split(':').Select(int.Parse).ToArray();
if (value.Length < 3) return TimeSpan.Zero;
var datetime = new TimeSpan(value[0], value[1], value[2]);
return datetime;
}