AABBbaby

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

界面控件DevExpress WinForms MVVM入门指南——登录表单(上)

获取工具下载 - 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线上公开课、中文教程资讯请上中文网获取

posted on   AABBbaby  阅读(140)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-04-20 DevExpress使用教程:手把手教你创建Blazor Dashboard应用
点击右上角即可分享
微信分享提示