[英]How to validate the number in Xamarin.Android with MVVMCross
I have the Xamarin.Android project with using MVVMCross. 我有使用MVVMCross的Xamarin.Android项目。 I work with Visual Studio.
我使用Visual Studio。
Windows 10 64 Pro
Visual Studio 2017
I need to create the text area for the number input and integrate basic validation (is this a number?) In case of error user should see the error message. 我需要为数字输入创建文本区域并集成基本验证(这是数字吗?)如果出现错误,用户应该会看到错误消息。
I have created the layout of this elements. 我已经创建了此元素的布局。 Area for the number and for the error message.
数字和错误消息的区域。
<?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" />
And here is NumberViewModel.cs
: 这是
NumberViewModel.cs
:
using MvvmCross.Platform.Converters;
using System;
using System.Collections.Generic;
using System.Globalization;
using My.project.Core.Models;
namespace My.project.Core.ViewModels
{
class NumberViewModel : BaseViewModel
{
private string _userNumber;
private string _incorrectNumber;
public string UserNumber
{
get
{
return _userNumber;
}
set
{
_userNumber = value;
this.RaisePropertyChanged(() => this.UserNumber);
this.RaisePropertyChanged(() => this.IncorrectNumber);
}
}
public string IncorrectNumber
{
get
{
if (UserNumber is Int16 || UserNumber is Int32)
{
return null;
}
else
{
return "Incorrect phone number";
}
}
}
}
}
But for some reason, I can't see the error message if I type non-numeric characters in this area. 但是由于某种原因,如果我在此区域键入非数字字符,则看不到错误消息。 It should appear under the number.
它应该出现在数字下方。
I'm the very newbie in MVVMCross and a little bit confused. 我是MVVMCross中的新手,有点困惑。 Please, help
请帮忙
UPD. UPD。 I have added changes from fmaccaroni advice.
我从fmaccaroni建议中添加了更改。 Now the error message is always here.
现在错误消息总是在这里。 Even if the area contains only digits.
即使该区域仅包含数字。
You can try to achieve it like this: 您可以尝试通过以下方式实现它:
int numb;
if (!int.TryParse(UserNumber, out numb) && UserNumber.Length != 0)
{
return "Incorrect phone number";
}
Your bindings are wrong, they should be local:MvxBind="Text UserNumber"
and local:MvxBind="Text IncorrectNumber"
check the docs . 您的绑定错误,它们应该是
local:MvxBind="Text UserNumber"
而local:MvxBind="Text IncorrectNumber"
检查文档 。
And unless you are using Fody.PropertyChanged package (which I'd recommend you to use) you should call this.RaisePropertyChanged(() => this.IncorrectNumber)
inside the setter of UserNumber, ie after _userNumber = value;
并且除非您使用Fody.PropertyChanged包(我建议您使用),否则应在
_userNumber = value;
的设置器内调用this.RaisePropertyChanged(() => this.IncorrectNumber)
,即在_userNumber = value;
in order to inform the View that some property ( IncorrectNumber
) has changed from the ViewModel 为了通知View某些属性(
IncorrectNumber
)已从ViewModel中更改
Update 更新
You should also change your validation in IncorrectNumber
setter as @mrisek said. 您还应该像@mrisek所说的那样在
IncorrectNumber
setter中更改您的验证。 It should be like 应该像
public string IncorrectNumber => !string.IsNullOrEmpty(UserNumber) && int.TryParse(UserNumber, out int n) ? null : "Incorrect phone number" ;
If you use the is
operator, it compares if the object is of that Type
which is not clearly. 如果使用
is
运算符,它将比较该对象是否属于该Type
(不清楚)。 By the way remember the length of an Int32 if you want a higher number you should use Int64 or make the validation with Regex: Regex.IsMatch(UserNumber, @"^\\d+$")
顺便说一句,请记住Int32的长度,如果您想使用更大的数字,则应使用Int64或使用Regex进行验证:
Regex.IsMatch(UserNumber, @"^\\d+$")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.