应用程序控件DevExpress WinForms MVVM入门指南——登录表单(下)
发布时间:2025年11月07日 12:19
上头请注意了 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 IfDevExpress WinForm
DevExpress WinForm保有180+框架和UI库,能为Windows Forms跨平台建立带有影响力的业务技术细节。DevExpress WinForms能完美协作流畅、质感且不易用作的客户端,无论是Office风格的图标,还是分析处理整机的业务数据,它都能轻松胜任!
。贵州男科医院挂号咨询沈阳妇科医院哪家最好
南京男科医院哪家好
南京看白癜风去哪家医院
海南白癜风医院怎么去
感冒
石家庄蓝天医院
瘦腰瑜珈
癫痫治疗
食品安全
- 美国5G基站影响飞行确保安全?海外航司紧急行动,取消部分赴美航班
- 国际钻石窄幅运行 今日关注初请数据
- 思域同平台打造:本田全新SUV车型谍照步出,将推混动版车型
- 应用程序控件DevExpress WinForms MVVM入门指南——登录表单(下)
- 新港静电发布2718系列数字硅唛,性能取得突破性提升
- 蔡嘉佑:黄金纸白银价格趋势分析及原油操作建议
- 特斯拉调回1.4万辆车:存在撞车风险 年内第三次调回
- 2022 阿里巴巴全球数学赛跑预赛第二、三题详解
- 独家!如何阿凡达数据分析?
- 数字不会撒谎!分析督导:模型暗示黄金“很便宜” 两大因素料将推升金价
- “高价铋”催涨业绩:12家公司净利翻倍,比亚迪也成赢家
- 自始期货今日观点:财政部长与美联储产生分歧,隔夜黄金迎来反弹
- 住酒店必备!华为内测“防窥视神器”,一键找出所有隐藏摄像头
- 北理工林程教授团队:电池冷凝研究综述
