【发布时间】:2018-01-16 14:42:29
【问题描述】:
我有使用 MVVMCross 的 Xamarin.Android 项目。我使用 Visual Studio。
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" />
这里是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";
}
}
}
}
}
但由于某种原因,如果我在此区域输入非数字字符,我将看不到错误消息。它应该出现在数字下方。
我是 MVVMCross 的新手,有点困惑。请帮忙
UPD。我添加了 fmaccaroni 建议的更改。 现在错误消息总是在这里。即使该区域仅包含数字。
【问题讨论】:
标签: android xamarin.android visual-studio-2017 mvvmcross