简体   繁体   English

如何使用MVVMCross在Xamarin.Android中验证数字

[英]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.

相关问题 如何使用Android模拟器模拟App Tombstoning? (适用于Xamarin.Android和MVVMCross) - How to simulate App Tombstoning with Android emulators? (for Xamarin.Android and MVVMCross) 通过Xamarin.Android中的MVVMCross绑定OxyPlot - Binding OxyPlot via MVVMCross in Xamarin.Android MVVMCROSS Xamarin.Android 6.1.2库导航 - MVVMCROSS Xamarin.Android 6.1.2 Library Navigation Xamarin.Android中带有下划线的文本和MVVMCross - Underline text in Xamarin.Android witn MVVMCross MvvmCross Xamarin.Android FloatingActionButton切换可见性 - MvvmCross Xamarin.Android FloatingActionButton toggle visibility 将多个值绑定到一个控件 Xamarin.Android MVMCross MVX - Bind multiple values to one control Xamarin.Android MVVMCross MVX Xamarin.Android + MVVMCross:升级到MVVMCross 6.3.1后DrawerLayout类冲突 - Xamarin.Android + MVVMCross: DrawerLayout class conflict after upgrading to MVVMCross 6.3.1 获取手机Xamarin.Android的号码? - Getting the number of the phone Xamarin.Android? 部署我的MVVMCross Xamarin.Android应用程序似乎清除了该应用程序的文档存储 - Deploying my MVVMCross Xamarin.Android application appears to clear the application's document store 使用国际化功能通过MvvmCross框架在Xamarin.Android上设置标题栏值 - Use internationalization to set the title bar value on Xamarin.Android with the MvvmCross framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM