DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

界面控件DevExpress WinForms MVVM使用教程(五):登录表单(上)

来源:   发布时间:2022-04-19   浏览:1167次

获取工具下载 - DevExpress v21.2

从本文档中,您将了解如何向应用程序添加登录表单。在本节教程中着重讨论了如何实现此任务,这基本上是附加应用程序功能的一部分。

1. 您的用户数据库实现方式可能会有所不同,对于示例应用程序,您可以定义以下简单类:

C#

public class User {
public string Login { get; set; }
public string Password { get; set; }
}

VB.NET

Public Class User
Public Property Login() As String
Public Property Password() As String
End 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 CredentialsSource

Private Sub New()
End Sub

Private Shared credentials As System.Collections.Hashtable
Shared 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 Sub
Friend Shared Function Check(ByVal login As String, ByVal pwd As String) As Boolean
Return Object.Equals(credentials(login), GetHash(pwd))
End Function
Private Shared Function GetHash(ByVal password As String) As Object
Return password
End Function
Friend Shared Iterator Function GetUserNames() As System.Collections.Generic.IEnumerable(Of String)
For Each item As String In credentials.Keys
Yield item
Next item
End Function
End Class

2. 使用 DataLayoutControl 创建一个 LoginView 用户控件,就像使用详细视图一样。 不要忘记将数据绑定的 DataSourceUpdateMode 设置为 OnPropertyChanged,否则按“Enter”键将传递一个空密码,因为编辑器仍处于焦点状态。 要提高登录表单的可用性,请使用编辑器的智能标签将显示用户名的编辑器类型更改为 LookUpEdit。

界面控件DevExpress WinForms MVVM使用教程(五):登录表单(上)

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.Generic
Imports DevExpress.Mvvm.POCO
Imports MVVMExpenses.Models

Public Class LoginViewModel
Public ReadOnly Property LookUpUsers() As IEnumerable(Of String)
Get
Return CredentialsSource.GetUserNames()
End Get
End Property
Public Overridable Property CurrentUser() As User
Private privateIsCurrentUserCredentialsValid As Boolean
Public Property IsCurrentUserCredentialsValid() As Boolean
Get
Return privateIsCurrentUserCredentialsValid
End Get
Private Set(ByVal value As Boolean)
privateIsCurrentUserCredentialsValid = value
End Set
End Property
'
<DevExpress.Mvvm.DataAnnotations.Command(False)>
Public Sub Init()
Me.CurrentUser = New User()
End Sub
Public Sub Update()
IsCurrentUserCredentialsValid = CredentialsSource.Check(CurrentUser.Login, CurrentUser.Password)
End Sub
Public Shared Function Create() As LoginViewModel
Return ViewModelSource.Create(Of LoginViewModel)()
End Function
End Class

在此 ViewModel 中,定义了两个属性:存储当前登录用户的 CurrentUser 属性和指定输入凭据是否已通过验证的布尔值 IsCurrentUserCredentialsValid 属性。

DevExpress WinForm | 下载试用

DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!

更多产品正版授权详情及优惠,欢迎咨询在线客服>>


DevExpress技术交流群6:600715373      欢迎一起进群讨论

更多DevExpress线上公开课、中文教程资讯请上中文网获取

DevExpress企业定制服务
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/3005.html

相关产品: DevExpress Universal Subscription,

在线
客服
微信
QQ 电话
023-68661681
返回
顶部