1. 程式人生 > 其它 >介面控制元件DevExpress WinForms MVVM入門指南——登入表單(上)

介面控制元件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。

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線上公開課、中文教程資訊請上中文網獲取