我有一个<Entry>类型的控件,其中我不希望用户输入以"0“开头的值,例如.
0 01 00 00010
但是如果我想输入以自然数形式包含"0“的值,例如.
10 2010年 200000
MyView.XAML
<Entry
HorizontalOptions="FillAndExpand"
Placeholder="Cantidad"
Keyboard="Numeric"
MaxLength="9"
Text="{Binding CantidadContenedor}"></Entry>MyViewModel.CS
string cantidadContenedor;
public string CantidadContenedor
{
get
{
return cantidadContenedor;
}
set
{
if (cantidadContenedor != value)
{
cantidadContenedor = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CantidadContenedor)));
}
}
}如何在捕获Entry的值之后添加此验证
我是否可以使用TryParse属性作为Container Quantity类型的string
对我有什么帮助吗?
发布于 2019-03-19 22:10:02
这个工作很完美!!
public MainPage()
{
InitializeComponent();
MyEntry.TextChanged+= MyEntry_TextChanged;
}
void MyEntry_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
{
if(!string.IsNullOrWhiteSpace(MyEntry.Text))
{
if (MyEntry.Text.StartsWith("0"))
MyEntry.Text = e.OldTextValue;
}
}发布于 2019-03-19 21:54:52
您可以将您所接收到的任何值作为string进行转换。
然后将第一个数字读入子字符串,如下所示:
using System;
public class Program
{
public static void Main()
{
string x = "0TEST";
if(x.StartsWith("0")){
Console.WriteLine("Starts with 0");
}else{
Console.WriteLine("Doesn't start with 0");
}
}
}然后写入逻辑以允许/拒绝它。
https://stackoverflow.com/questions/55250492
复制相似问题