1. 您的用户数据库实现方式可能会有所不同,对于示例应用程序,您可以定义以下简单类:
C#
public class User {public string Login { get; set; }public string Password { get; set; }}
VB.NET
Public Class UserPublic Property Login() As StringPublic Property Password() As StringEnd Class
…以及以下存储用户凭据的类。
C#
static class CredentialsSource {static System.Collections.Hashtable credentials;static CredentialsSource() {credentials = new System.Collections.Hashtable();credentials.Add("Guest", GetHash(null));credentials.Add("John", GetHash("qwerty"));credentials.Add("Administrator", GetHash("admin"));credentials.Add("Mary", GetHash("12345"));}internal static bool Check(string login, string pwd) {return object.Equals(credentials[login], GetHash(pwd));}static object GetHash(string password) {return password;}internal static System.Collections.Generic.IEnumerable<string> GetUserNames() {foreach(string item in credentials.Keys)yield return item;}}
VB.NET
Friend NotInheritable Class CredentialsSourcePrivate Sub New()End SubPrivate Shared credentials As System.Collections.HashtableShared Sub New()credentials = New System.Collections.Hashtable()credentials.Add("Guest", GetHash(Nothing))credentials.Add("John", GetHash("qwerty"))credentials.Add("Administrator", GetHash("admin"))credentials.Add("Mary", GetHash("12345"))End SubFriend Shared Function Check(ByVal login As String, ByVal pwd As String) As BooleanReturn Object.Equals(credentials(login), GetHash(pwd))End FunctionPrivate Shared Function GetHash(ByVal password As String) As ObjectReturn passwordEnd FunctionFriend Shared Iterator Function GetUserNames() As System.Collections.Generic.IEnumerable(Of String)For Each item As String In credentials.KeysYield itemNext itemEnd FunctionEnd Class
3. 对于本次登录View相关的ViewModel,可以使用Scaffolding Wizard生成或者手动实现,以下代码说明了最简单的 LoginViewModel 实现。
C#
using System.Collections.Generic;using DevExpress.Mvvm.POCO;using MVVMExpenses.Models;public class LoginViewModel {public IEnumerable<string> LookUpUsers {get { return CredentialsSource.GetUserNames(); }}public virtual User CurrentUser { get; set; }public bool IsCurrentUserCredentialsValid { get; private set; }//[DevExpress.Mvvm.DataAnnotations.Command(false)]public void Init() {this.CurrentUser = new User();}public void Update() {IsCurrentUserCredentialsValid = CredentialsSource.Check(CurrentUser.Login, CurrentUser.Password);}public static LoginViewModel Create() {return ViewModelSource.Create<LoginViewModel>();}}
VB.NET
Imports System.Collections.GenericImports DevExpress.Mvvm.POCOImports MVVMExpenses.ModelsPublic Class LoginViewModelPublic ReadOnly Property LookUpUsers() As IEnumerable(Of String)GetReturn CredentialsSource.GetUserNames()End GetEnd PropertyPublic Overridable Property CurrentUser() As UserPrivate privateIsCurrentUserCredentialsValid As BooleanPublic Property IsCurrentUserCredentialsValid() As BooleanGetReturn privateIsCurrentUserCredentialsValidEnd GetPrivate Set(ByVal value As Boolean)privateIsCurrentUserCredentialsValid = valueEnd SetEnd Property'<DevExpress.Mvvm.DataAnnotations.Command(False)>Public Sub Init()Me.CurrentUser = New User()End SubPublic Sub Update()IsCurrentUserCredentialsValid = CredentialsSource.Check(CurrentUser.Login, CurrentUser.Password)End SubPublic Shared Function Create() As LoginViewModelReturn ViewModelSource.Create(Of LoginViewModel)()End FunctionEnd Class
在此 ViewModel 中,定义了两个属性:存储当前登录用户的 CurrentUser 属性和指定输入凭据是否已通过验证的布尔值 IsCurrentUserCredentialsValid 属性。
DevExpress WinForm | 下载试用
DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!
更多产品正版授权详情及优惠,欢迎咨询在线客服>>
DevExpress技术交流群6:600715373 欢迎一起进群讨论

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!