首页 >> 中医拔罐

应用程序控件DevExpress WinForms MVVM入门指南——登录表单(下)

发布时间:2025年11月07日 12:19

opertyProtected ReadOnly Property MessageService() As IMessageBoxServiceGetReturn Me.GetService(Of IMessageBoxService)()End GetEnd PropertyPublic Overrides Sub OnLoaded(ByVal [module] As MyDbContextModuleDescription)MyBase.OnLoaded([module])Login()End SubPublic Overridable Property State() As AppState' Shows the Login ViewPublic Sub Login()OnLogin(DialogService.ShowDialog(MessageButton.OKCancel, "Please enter you credentials", "LoginView", loginViewModel))End Sub'Occurs whenever the end-user clicks a dialog buttonPrivate Sub OnLogin(ByVal result As MessageResult)If result Is MessageResult.Cancel ThenState = AppState.ExitQueuedElseIf loginViewModel.IsCurrentUserCredentialsValid ThenState = AppState.AutorizedElseLogin()End IfEnd IfEnd SubProtected Sub OnStateChanged()Me.RaiseCanExecuteChanged(Sub(x) x.Logout())If State = AppState.Autorized ThenMessenger.Default.Send(Of String)(loginViewModel.CurrentUser.Login)ElseMessenger.Default.Send(Of String)(String.Empty)End IfEnd SubEnd ClassPublic Enum AppStateNotAutorizedAutorizedExitQueuedEnd Enum

上头请注意了 LoginViewModel 和两个镜像(MainView 和 LoginView)的code。 当您的 ViewModel 准备就绪时,继续协作项目并将 MvvmContext 框架加进到录入URL中才会,用作其智能标签将 LoginViewModel 分配为此镜像的相关镜像三维。

C#

//LoginViewModel.cspublic class LoginViewModel {public IEnumerable 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();}}//MainView.cspublic MainView() {InitializeComponent();this.Opacity = 0;. . .}void InitializeNavigation() {. . .var fluentAPI = mvvmContext1.OfType();fluentAPI.SetTrigger(x => x.State, (state) =>{if(state == AppState.Autorized)Opacity = 1; /*Show Main Form*/if(state == AppState.ExitQueued)Close(); // exit the app;});}//LoginView.cspublic partial class LoginView : DevExpress.XtraEditors.XtraUserControl {public LoginView() {InitializeComponent();}protected override void OnLoad(System.EventArgs e) {base.OnLoad(e);var fluentAPI = mvvmContext1.OfType();fluentAPI.SetObjectDataSourceBinding(userBindingSource,x => x.CurrentUser, x => x.Update());foreach(string item in mvvmContext1.GetViewModel().LookUpUsers)LoginTextEdit.Properties.Items.Add(item);fluentAPI.ViewModel.Init();}}

VB.NET

'LoginViewModel.vbPublic 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 PropertyPublic 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'MainView.vbPublic Sub New()InitializeComponent()Me.Opacity = 0. . .End SubPrivate Sub InitializeNavigation(). . .Dim fluentAPI = mvvmContext1.OfType(Of MyDbContextViewModel)()fluentAPI.SetTrigger(Function(x) x.State, Sub(state)If state = AppState.Autorized ThenOpacity = 1End IfIf state = AppState.ExitQueued ThenClose()End IfEnd Sub) ' exit the app; - Show Main FormEnd Sub'LoginView.vbPartial Public Class LoginViewInherits DevExpress.XtraEditors.XtraUserControlPublic Sub New()InitializeComponent()End SubProtected Overrides Sub OnLoad(ByVal e As System.EventArgs)MyBase.OnLoad(e)Dim fluentAPI = mvvmContext1.OfType(Of LoginViewModel)()fluentAPI.SetObjectDataSourceBinding(userBindingSource, Function(x) x.CurrentUser, Function(x) x.Update())For Each item As String In mvvmContext1.GetViewModel(Of LoginViewModel)().LookUpUsersLoginTextEdit.Properties.Items.Add(item)Next itemfluentAPI.ViewModel.Init()End SubEnd Class

此code用作 OnLoaded 解决办法货运来表明用作已注册 DialogService 的对话框,为此Login解决办法函数调用服务的ShowDialog扩展解决办法,此解决办法将子 ViewModel 作为变量 - 将 LoginViewModel 类的新范例传递给它。建立这个范例很重要,不是用作 new 页面,而是函数调用 ViewModelSource.Create 解决办法。或者,您可以函数调用 SetParentViewModel 解决办法为此范例特设父 ViewModel。

当最终浏览器单击任何录入对话框的屏幕时,此通告结果将传递给 OnLogin 解决办法,该解决办法才会精准检查单击了哪个屏幕。 如果最终浏览器单击 ‘Cancel’ 或停用对话框,则客户端将停用。如果单击‘OK’屏幕,客户端将检查 IsCurrentUserCredentialsValid 表征,该表征才会在函数调用 Update 解决办法时系统会刷新其值。如果转换成的具名有效地,将表明亦然URL,否则将继续表明录入URL,这是通过为 State 表征分配不同的值来启动的。 MainView 有一个比较机,用于情报搜集 State 表征值的变化,并在它再次发生时做出相应的反应。

5. 前面的解决办法所能充分利用带有最少动态的录入URL。 但是,如果您的亦然镜像分配了停用推定操作者,可能才会遇到某些弊端。 例如,如果您停用录入URL,亦然URL(由于未转换成有效地具名而变得表面)也将试着自行停用。 这将表明推定通告,如果您单击‘Cancel’屏幕,注记将原有,但您将看不到它。 要借助此类弊端,请删除URL停用操作者(如果有)并加进表列code。

C#

//MainView.csfluentAPI.WithEvent(this, "FormClosing").EventToCommand(x => x.OnClosing(null), new Func((args) => args));//MyDbContextViewModel.partial.cspublic override void OnClosing(CancelEventArgs cancelEventArgs) {base.OnClosing(cancelEventArgs);if(!cancelEventArgs.Cancel) {if(State == AppState.Autorized && MessageService.ShowMessage("Do you really want to close the application?", "Confirm", MessageButton.YesNo) == MessageResult.No)cancelEventArgs.Cancel = true;}}

VB.NET

'MainView.vbfluentAPI.WithEvent(Of FormClosingEventArgs)(Me, "FormClosing").EventToCommand(Function(x) x.OnClosing(Nothing), New Func(Of CancelEventArgs, Object)(Function(args) args))'MyDbContextViewModel.partial.vbpublic override void OnClosing(CancelEventArgs cancelEventArgs)MyBase.OnClosing(cancelEventArgs)If Not cancelEventArgs.Cancel ThenIf State = AppState.Autorized AndAlso MessageService.ShowMessage("Do you really want to close the application?", "Confirm", MessageButton.YesNo) = MessageResult.No ThencancelEventArgs.Cancel = TrueEnd IfEnd If

此code检查这两项的 State 表征值,仅在授权通过时表明推定通告。 如果最终浏览器尚未录入并决定停用客户端,则不才会表明任何推定信息。 这就是为什么 State 表征不是形式化,而是不能接受快捷键 AppState 枚举机的值的原因。 可能共存三种分析方法情况:

Authorized(已授权) - 浏览器具名有效地。 亦然URL是可见的,试着停用它不应才会表明推定通告,最终浏览器可以单击 ‘No’ 来保持客户端运行。NotAuthorized - 转换成了浏览器具名,但未通过检验。 亦然客户端URL保持表面,录入URL继续表明。ExitQueued - 未转换成浏览器具名,录入URL已停用,客户端应在没有任何推定对话框的情况取消。

6. 您的录入URL现今准备就绪。可以通过为密码编辑机特设特定的 RepositoryItemTextEdit.PasswordChar 来室内装饰它,在亦然URL上反映录入浏览器的名称,并将屏幕加进到亦然镜像的交叉控件中才会,以便您继续录入等,上头的code阐明 怎么做。

C#

//LoginView.csPasswordTextEdit.Properties.PasswordChar = '*';//MyDbContextViewModel.partial.csprotected void OnStateChanged() {this.RaiseCanExecuteChanged(x => x.Logout());if(State == AppState.Authorized)Messenger.Default.Send(loginViewModel.CurrentUser.Login);elseMessenger.Default.Send(string.Empty);}public void Logout() {State = AppState.ExitQueued;System.Diagnostics.Process.Start(System.Windows.Forms.Application.ExecutablePath);}public bool CanLogout() {return State == AppState.Authorized;}//MainView.csMessenger.Default.Register(this, OnUserNameMessage);fluentAPI.BindCommand(biLogout, x => x.Logout());void OnUserNameMessage(string userName) {if(string.IsNullOrEmpty(userName))this.Text = "Expenses Application";elsethis.Text = "Expenses Application - (" + userName + ")";}

VB.NET

'LoginView.vbPasswordTextEdit.Properties.PasswordChar = "*"c'MyDbContextViewModel.partial.vbprotected void OnStateChanged()Me.RaiseCanExecuteChanged(Sub(x) x.Logout())If State = AppState.Authorized ThenMessenger.Default.Send(Of String)(loginViewModel.CurrentUser.Login)ElseMessenger.Default.Send(Of String)(String.Empty)End Ifpublic void Logout()State = AppState.ExitQueuedSystem.Diagnostics.Process.Start(System.Windows.Forms.Application.ExecutablePath)public Boolean CanLogout()Return State = AppState.Authorized'MainView.vbMessenger.Default.Register(Of String)(Me, AddressOf OnUserNameMessage)fluentAPI.BindCommand(biLogout, Function(x) x.Logout())void OnUserNameMessage(String userName)If String.IsNullOrEmpty(userName) ThenMe.Text = "Expenses Application"ElseMe.Text = "Expenses Application - (" & userName & ")"End If

DevExpress WinForm

DevExpress WinForm保有180+框架和UI库,能为Windows Forms跨平台建立带有影响力的业务技术细节。DevExpress WinForms能完美协作流畅、质感且不易用作的客户端,无论是Office风格的图标,还是分析处理整机的业务数据,它都能轻松胜任!

贵州男科医院挂号咨询
沈阳妇科医院哪家最好
南京男科医院哪家好
南京看白癜风去哪家医院
海南白癜风医院怎么去
感冒
石家庄蓝天医院
瘦腰瑜珈
癫痫治疗
食品安全

上一篇: 磷酸铁锂的产品报价动态(2022-01-07)

下一篇: 新港静电发布2718系列数字硅唛,性能取得突破性提升

友情链接