如何使用MVVMCross验证Xamarin.Android中的数字
我有一个使用MVVMCross的Xamarin.Android项目。我在VisualStudio工作如何使用MVVMCross验证Xamarin.Android中的数字,android,xamarin.android,visual-studio-2017,mvvmcross,Android,Xamarin.android,Visual Studio 2017,Mvvmcross,我有一个使用MVVMCross的Xamarin.Android项目。我在VisualStudio工作 Windows 10 64 Pro Visual Studio 2017 我需要为数字输入创建文本区域,并集成基本验证(这是一个数字吗?)以防出现错误,用户应该会看到错误消息 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/
Windows 10 64 Pro
Visual Studio 2017
我需要为数字输入创建文本区域,并集成基本验证(这是一个数字吗?)以防出现错误,用户应该会看到错误消息
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
<EditText
android:id="@+id/userNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="10"
android:phoneNumber="true"
local:MvxBind="Text UserNumber" />
<TextView
android:id="@+id/incorrectNumber"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#F44336"
local:MvxBind="Text IncorrectNumber" />
我已经创建了这些元素的布局。用于显示号码和错误消息的区域
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
<EditText
android:id="@+id/userNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="10"
android:phoneNumber="true"
local:MvxBind="Text UserNumber" />
<TextView
android:id="@+id/incorrectNumber"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#F44336"
local:MvxBind="Text IncorrectNumber" />
但由于某些原因,如果在该区域键入非数字字符,则无法看到错误消息。它应该出现在数字下面
我是MVVMCross的新手,有点困惑。求求你,救命
UPD。我添加了来自fmaccaroni建议的更改。
现在错误消息总是在这里。即使该区域仅包含数字。您也可以尝试这样实现:
int numb;
if (!int.TryParse(UserNumber, out numb) && UserNumber.Length != 0)
{
return "Incorrect phone number";
}
您的绑定错误,它们应该是
local:MvxBind=“Text UserNumber”
和local:MvxBind=“Text IncorrectNumber”
检查
除非您正在使用包(我建议您使用),否则您应该在UserNumber的setter内调用this.RaisePropertyChanged(()=>this.IncorrectNumber)
,即在之后调用UserNumber=value
以通知视图某些属性(不正确编号
)已从ViewModel更改
更新
正如@mrisek所说,您还应该在IncorrectNumber
setter中更改验证。
应该是
公共字符串不正确编号=>!string.IsNullOrEmpty(UserNumber)和&int.TryParse(UserNumber,out int n)?空:“电话号码不正确”代码>
如果使用is
操作符,它会比较对象是否属于不清楚的类型。
顺便说一句,记住Int32的长度如果你想要一个更高的数字,你应该使用Int64或使用Regex进行验证:Regex.IsMatch(UserNumber,@“^\d+$”)
谢谢!我修复了我的代码并进行了测试。现在错误消息总是在这里。即使该区域仅包含数字。(我在帖子中添加了代码更改)在UserNumber setter中缺少this.RaisePropertyChanged(()=>this.IncorrectNumber)
,即在this.RaisePropertyChanged(()=>this.UserNumber)之后代码>。此RaisePropertyChanged将让视图知道不正确编号
已更改,因此它将再次进入get
更新视图。顺便说一句,你不需要在IncorrectNumber
中设置一个setter,当它从来没有被设置好的时候,我做了。但错误仍然存在。即使该区域只包含数字,我也能看到它。也许,转换器出了什么问题?@NaSt用不正确的数字验证更新了答案是的,它起作用了!非常感谢。最后一个问题:首先,文本区域是空的,不正确的数字显示了错误。如何为空UserNumber添加异常?此解决方案有效!只有一个问题。首先,文本区域为空,IncorrectNumber向我显示错误。如何为空UserNumber添加异常?我编辑了答案,因此如果输入的长度等于零,则不会显示错误消息。