uniapp表单验证插件_Xamarin.Forms登录对话框及表单验证

目前只实现了弹出登录窗口及窗口表单验证

微信公众号:Dotnet9,网站:Dotnet9,问题或建议,请网站留言; 如果您觉得Dotnet9对您有帮助,欢迎赞赏。

内容目录

  1. 实现效果
  2. 业务场景
  3. 编码实现
  4. 本文参考
  5. 源码下载

1.实现效果

d9e8720741d6173c81bf12030dff4008.png

弹出登录窗口,输入验证

2.业务场景

  1. 主窗口弹出登录或者其他小窗口
  2. 表单验证

3.编码实现

3.1 添加Nuget库

创建名为 “LoginSystem” 的Xamarin.Forms项目,添加2个Nuget库

  1. Rg.Plugins.Popup 1.2.0.223:弹出框由该插件提供
  2. FluentValidation 8.6.1:表单验证使用

3.2 工程结构

f783750ca487d65de17cffd06f7f4b39.png

3.3 共享库中的MainPage

弹出登录窗口,MainPage.xaml中关键代码

后台弹出登录窗口 MainPage.xaml.cs

private async void Login_Click(object sender, EventArgs e){    await PopupNavigation.Instance.PushAsync(new LoginPage());}

3.4 Models中类文件

3.4.1 LoginUser.cs

namespace LoginSystem.Models{    class LoginUser    {        public string UserName { get; set; }        public string Password { get; set; }    }}

3.4.2 LoginUserValidator.cs

使用FluentValidation进行实体规则验证

using FluentValidation;namespace LoginSystem.Models{    class LoginUserValidator : AbstractValidator    {        public LoginUserValidator()        {            RuleFor(x => x.UserName).NotEmpty().WithMessage("请输入账号")                .Length(5, 20).WithMessage("账号长度在5到20个字符之间");            RuleFor(x => x.Password).NotEmpty().WithMessage("请输入密码");        }    }}


3.4.3 NotifyPropertyChanged.cs

封装INotifyPropertyChanged接口

using System;using System.Collections.Generic;using System.ComponentModel;using System.Runtime.CompilerServices;namespace LoginSystem.Models{    public class NotifyPropertyChanged : INotifyPropertyChanged    {        protected bool SetProperty(ref T backingStore, T value,            [CallerMemberName]string propertyName = "",            Action onChanged = null)        {            if (EqualityComparer.Default.Equals(backingStore, value))                return false;            backingStore = value;            onChanged?.Invoke();            OnPropertyChanged(propertyName);            return true;        }        #region INotifyPropertyChanged        public event PropertyChangedEventHandler PropertyChanged;        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")        {            var changed = PropertyChanged;            if (changed == null)                return;            changed.Invoke(this, new PropertyChangedEventArgs(propertyName));        }        #endregion    }}


3.5 ViewModel中类文件

3.5.1 LoginViewModel.cs

登录视图的ViewModel,FluentValidation的具体调用

using FluentValidation;using LoginSystem.Models;using System;using System.Threading.Tasks;using System.Windows.Input;using Xamarin.Forms;namespace LoginSystem.ViewModel{    class LoginViewModel : NotifyPropertyChanged    {        public INavigation Navigation { get; set; }        public LoginUser LoginUserIns { get; set; }        string userName = string.Empty;        public string UserName        {            get { return userName; }            set { SetProperty(ref userName, value); }        }        string password = string.Empty;        public string Password        {            get { return password; }            set { SetProperty(ref password, value); }        }        private readonly IValidator _validator;        public LoginViewModel()        {            _validator = new LoginUserValidator();        }        private ICommand loginCommand;        public ICommand LoginCommand        {            get            {                return loginCommand ?? (loginCommand = new Command(ExecuteLoginCommand));            }        }        private string validateMsg;        public string ValidateMsg        {            get            {                return validateMsg;            }            set            {                SetProperty(ref validateMsg, value);            }        }        private async void ExecuteLoginCommand(object obj)        {            try            {                if (LoginUserIns == null)                {                    LoginUserIns = new LoginUser();                }                LoginUserIns.UserName = userName;                LoginUserIns.Password = password;                var validationResult = _validator.Validate(LoginUserIns);                if (validationResult.IsValid)                {                    //TODO 作服务端登录验证                    ValidateMsg = "登录成功!";                }                else                {                    if (validationResult.Errors.Count > 0)                    {                        ValidateMsg = validationResult.Errors[0].ErrorMessage;                    }                    else                    {                        ValidateMsg = "登录失败!";                    }                }            }            catch (Exception ex)            {                ValidateMsg = ex.Message;            }            finally            {            }            await Task.FromResult("");        }    }}


3.6 Views中的视图文件

3.6.1 LoginPage

登录窗口LoginPage.xaml,引入弹出插件Rg.Plugins.Popup,设置弹出框动画,绑定FluentValidation验证提示信息 “ValidateMsg”

<?xml version="1.0" encoding="utf-8" ?>#2196F3                                            

后台LoginPage.xaml.cs绑定ViewModel LoginViewModel,需要设置Navigation到LoginViewModel的属性Navigation,用于ViewModel中验证成功时返回主窗口使用

using LoginSystem.ViewModel;using Rg.Plugins.Popup.Pages;using Xamarin.Forms.Xaml;namespace LoginSystem.Views{    [XamlCompilation(XamlCompilationOptions.Compile)]    public partial class LoginPage : PopupPage    {        LoginViewModel ViewModel = null;        public LoginPage()        {            if (ViewModel == null)            {                ViewModel = new LoginViewModel();            }            this.BindingContext = ViewModel;            ViewModel.Navigation = Navigation;            InitializeComponent();        }    }}

3.7 Android项目中的MainActivity.cs

214412b97fe595bb3f833135d37ce883.png

注册弹出插件

3.8 iOS项目中的AppDelegate.cs

7183653b378bb334a4cd3672132e711c.png

注册弹出插件

4.本文参考

Houssem Dellai 大神的学习视频:Popup in Xamarin Forms

Fluent Validation With MVVM In Xamarin Forms Application

5.代码下载

文中代码已经全部提供

除非注明,文章均由 Dotnet9 整理发布,欢迎转载。

转载请注明本文地址:https://dotnet9.com/6841.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值