I have the Xamarin.Android project with using MVVMCross. I work with 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
:
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. Please, help
UPD. I have added changes from fmaccaroni advice. 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 .
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;
in order to inform the View that some property ( IncorrectNumber
) has changed from the ViewModel
Update
You should also change your validation in IncorrectNumber
setter as @mrisek said. 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. 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+$")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.