界面控件DevExpressWinFormsMVVM入门指南
从本文档中,您将了解如何向应用程序添加登录表单。在本节教程中着重讨论了如何实现此任务,这基本上是附加应用程序功能的一部分。
DevExpressUniversalSubscription官方最新版免费下载试用,历史版本下载,在线文档和帮助文件下载慧都网
4。您还需要向主表单的ViewModel中添加一些代码。由于主表单使用自动生成的MyDbContextViewModel类,因此不建议将自定义代码直接添加到其中如果您需要再次调用ScaffoldingWizard,可以重新生成模型。相反,创建一个位于单独文件中的部分类。请注意,您必须将类构造函数从其原始文件移动到这个分部类。
CMyDbContextViewModel。partial。cspublicpartialclassMyDbContextViewModel{LoginViewModelloginViewModel;protectedMyDbContextViewModel():base(UnitOfWorkSource。GetUnitOfWorkFactory()){loginViewModelLoginViewModel。Create();loginViewModel。SetParentViewModel(this);}protectedIDialogServiceDialogService{get{returnthis。GetServiceIDialogService();}}protectedIMessageBoxServiceMessageService{get{returnthis。GetServiceIMessageBoxService();}}publicoverridevoidOnLoaded(MyDbContextModuleDescriptionmodule){base。OnLoaded(module);Login();}publicvirtualAppStateState{get;set;}ShowstheLoginViewpublicvoidLogin(){OnLogin(DialogService。ShowDialog(MessageButton。OKCancel,Pleaseenteryoucredentials,LoginView,loginViewModel));}OccurswhenevertheenduserclicksadialogbuttonvoidOnLogin(MessageResultresult){if(resultMessageResult。Cancel)StateAppState。ExitQueued;else{if(loginViewModel。IsCurrentUserCredentialsValid)StateAppState。Autorized;elseLogin();}}protectedvoidOnStateChanged(){this。RaiseCanExecuteChanged(xx。Logout());if(StateAppState。Autorized)Messenger。Default。Sendstring(loginViewModel。CurrentUser。Login);elseMessenger。Default。Sendstring(string。Empty);}}publicenumAppState{NotAutorized,Autorized,ExitQueued}
VB。NETMyDbContextViewModel。partial。vbPartialPublicClassMyDbContextViewModelPrivateloginViewModelAsLoginViewModelProtectedSubNew()MyBase。New(UnitOfWorkSource。GetUnitOfWorkFactory())loginViewModelLoginViewModel。Create()loginViewModel。SetParentViewModel(Me)EndSubProtectedReadOnlyPropertyDialogService()AsIDialogServiceGetReturnMe。GetService(OfIDialogService)()EndGetEndPropertyProtectedReadOnlyPropertyMessageService()AsIMessageBoxServiceGetReturnMe。GetService(OfIMessageBoxService)()EndGetEndPropertyPublicOverridesSubOnLoaded(ByVal〔module〕AsMyDbContextModuleDescription)MyBase。OnLoaded(〔module〕)Login()EndSubPublicOverridablePropertyState()AsAppStateShowstheLoginViewPublicSubLogin()OnLogin(DialogService。ShowDialog(MessageButton。OKCancel,Pleaseenteryoucredentials,LoginView,loginViewModel))EndSubOccurswhenevertheenduserclicksadialogbuttonPrivateSubOnLogin(ByValresultAsMessageResult)IfresultIsMessageResult。CancelThenStateAppState。ExitQueuedElseIfloginViewModel。IsCurrentUserCredentialsValidThenStateAppState。AutorizedElseLogin()EndIfEndIfEndSubProtectedSubOnStateChanged()Me。RaiseCanExecuteChanged(Sub(x)x。Logout())IfStateAppState。AutorizedThenMessenger。Default。Send(OfString)(loginViewModel。CurrentUser。Login)ElseMessenger。Default。Send(OfString)(String。Empty)EndIfEndSubEndClassPublicEnumAppStateNotAutorizedAutorizedExitQueuedEndEnum
下面列出了LoginViewModel和两个视图(MainView和LoginView)的代码。当您的ViewModel准备就绪时,重新构建项目并将MvvmContext组件添加到登录表单中,使用其智能标签将LoginViewModel分配为此视图的相关视图模型。
CLoginViewModel。cspublicclassLoginViewModel{publicIEnumerablestringLookUpUsers{get{returnCredentialsSource。GetUserNames();}}publicvirtualUserCurrentUser{get;set;}publicboolIsCurrentUserCredentialsValid{get;privateset;}〔DevExpress。Mvvm。DataAnnotations。Command(false)〕publicvoidInit(){this。CurrentUsernewUser();}publicvoidUpdate(){IsCurrentUserCredentialsValidCredentialsSource。Check(CurrentUser。Login,CurrentUser。Password);}publicstaticLoginViewModelCreate(){returnViewModelSource。CreateLoginViewModel();}}MainView。cspublicMainView(){InitializeComponent();this。Opacity0;。。。}voidInitializeNavigation(){。。。varfluentAPImvvmContext1。OfTypeMyDbContextViewModel();fluentAPI。SetTrigger(xx。State,(state){if(stateAppState。Autorized)Opacity1;ShowMainFormif(stateAppState。ExitQueued)Close();exittheapp;});}LoginView。cspublicpartialclassLoginView:DevExpress。XtraEditors。XtraUserControl{publicLoginView(){InitializeComponent();}protectedoverridevoidOnLoad(System。EventArgse){base。OnLoad(e);varfluentAPImvvmContext1。OfTypeLoginViewModel();fluentAPI。SetObjectDataSourceBinding(userBindingSource,xx。CurrentUser,xx。Update());foreach(stringiteminmvvmContext1。GetViewModelLoginViewModel()。LookUpUsers)LoginTextEdit。Properties。Items。Add(item);fluentAPI。ViewModel。Init();}}
VB。NETLoginViewModel。vbPublicClassLoginViewModelPublicReadOnlyPropertyLookUpUsers()AsIEnumerable(OfString)GetReturnCredentialsSource。GetUserNames()EndGetEndPropertyPublicOverridablePropertyCurrentUser()AsUserPrivateprivateIsCurrentUserCredentialsValidAsBooleanPublicPropertyIsCurrentUserCredentialsValid()AsBooleanGetReturnprivateIsCurrentUserCredentialsValidEndGetPrivateSet(ByValvalueAsBoolean)privateIsCurrentUserCredentialsValidvalueEndSetEndPropertyDevExpress。Mvvm。DataAnnotations。Command(False)PublicSubInit()Me。CurrentUserNewUser()EndSubPublicSubUpdate()IsCurrentUserCredentialsValidCredentialsSource。Check(CurrentUser。Login,CurrentUser。Password)EndSubPublicSharedFunctionCreate()AsLoginViewModelReturnViewModelSource。Create(OfLoginViewModel)()EndFunctionEndClassMainView。vbPublicSubNew()InitializeComponent()Me。Opacity0。。。EndSubPrivateSubInitializeNavigation()。。。DimfluentAPImvvmContext1。OfType(OfMyDbContextViewModel)()fluentAPI。SetTrigger(Function(x)x。State,Sub(state)IfstateAppState。AutorizedThenOpacity1EndIfIfstateAppState。ExitQueuedThenClose()EndIfEndSub)exittheapp;ShowMainFormEndSubLoginView。vbPartialPublicClassLoginViewInheritsDevExpress。XtraEditors。XtraUserControlPublicSubNew()InitializeComponent()EndSubProtectedOverridesSubOnLoad(ByValeAsSystem。EventArgs)MyBase。OnLoad(e)DimfluentAPImvvmContext1。OfType(OfLoginViewModel)()fluentAPI。SetObjectDataSourceBinding(userBindingSource,Function(x)x。CurrentUser,Function(x)x。Update())ForEachitemAsStringInmvvmContext1。GetViewModel(OfLoginViewModel)()。LookUpUsersLoginTextEdit。Properties。Items。Add(item)NextitemfluentAPI。ViewModel。Init()EndSubEndClass
此代码使用OnLoaded方法重载来显示使用已注册DialogService的对话框,为此Login方法调用服务的ShowDialog扩展方法,此方法将子ViewModel作为参数将LoginViewModel类的新实例传递给它。创建这个实例很重要,不是使用new关键字,而是调用ViewModelSource。Create方法。或者,您可以调用SetParentViewModel方法为此实例设置父ViewModel。
当最终用户单击任何登录对话框的按钮时,此消息结果将传递给OnLogin方法,该方法会准确检查单击了哪个按钮。如果最终用户单击‘Cancel’或关闭对话框,则应用程序将关闭。如果单击‘OK’按钮,应用程序将检查IsCurrentUserCredentialsValid属性,该属性会在调用Update方法时自动刷新其值。如果输入的凭据有效,将显示主表单,否则将重新显示登录表单,这是通过为State属性分配不同的值来完成的。MainView有一个触发器,用于监视State属性值的变化,并在它发生时做出相应的反应。
5。前面的步骤足以实现具有最少功能的登录表单。但是,如果您的主视图分配了关闭确认操作,可能会遇到某些问题。例如,如果您关闭登录表单,主表单(由于未输入有效凭据而变得透明)也将尝试自行关闭。这将显示确认消息,如果您单击‘Cancel’按钮,表格将保留,但您将看不到它。要克服此类问题,请删除表单关闭操作(如果有)并添加以下代码。
CMainView。csfluentAPI。WithEventFormClosingEventArgs(this,FormClosing)。EventToCommand(xx。OnClosing(null),newFuncCancelEventArgs,object((args)args));MyDbContextViewModel。partial。cspublicoverridevoidOnClosing(CancelEventArgscancelEventArgs){base。OnClosing(cancelEventArgs);if(!cancelEventArgs。Cancel){if(StateAppState。AutorizedMessageService。ShowMessage(Doyoureallywanttoclosetheapplication?,Confirm,MessageButton。YesNo)MessageResult。No)cancelEventArgs。Canceltrue;}}
VB。NETMainView。vbfluentAPI。WithEvent(OfFormClosingEventArgs)(Me,FormClosing)。EventToCommand(Function(x)x。OnClosing(Nothing),NewFunc(OfCancelEventArgs,Object)(Function(args)args))MyDbContextViewModel。partial。vbpublicoverridevoidOnClosing(CancelEventArgscancelEventArgs)MyBase。OnClosing(cancelEventArgs)IfNotcancelEventArgs。CancelThenIfStateAppState。AutorizedAndAlsoMessageService。ShowMessage(Doyoureallywanttoclosetheapplication?,Confirm,MessageButton。YesNo)MessageResult。NoThencancelEventArgs。CancelTrueEndIfEndIf
此代码检查当前的State属性值,仅在授权通过时显示确认消息。如果最终用户尚未登录并决定关闭应用程序,则不会显示任何确认信息。这就是为什么State属性不是布尔值,而是接受自定义AppState枚举器的值的原因。可能存在三种应用状态:Authorized(已授权)用户凭据有效。主表单是可见的,尝试关闭它应该会显示确认消息,最终用户可以单击‘No’来保持应用程序运行。NotAuthorized输入了用户凭据,但未通过验证。主应用程序表单保持透明,登录表单重新显示。ExitQueued未输入用户凭据,登录表单已关闭,应用程序应在没有任何确认对话框的情况下终止。
6。您的登录表单现已准备就绪。可以通过为密码编辑器设置特定的RepositoryItemTextEdit。PasswordChar来装饰它,在主表单上反映登录用户的名称,并将按钮添加到主视图的网格控件中,以便您重新登录等,下面的代码说明了怎么做。
CLoginView。csPasswordTextEdit。Properties。PasswordChar;MyDbContextViewModel。partial。csprotectedvoidOnStateChanged(){this。RaiseCanExecuteChanged(xx。Logout());if(StateAppState。Authorized)Messenger。Default。Sendstring(loginViewModel。CurrentUser。Login);elseMessenger。Default。Sendstring(string。Empty);}publicvoidLogout(){StateAppState。ExitQueued;System。Diagnostics。Process。Start(System。Windows。Forms。Application。ExecutablePath);}publicboolCanLogout(){returnStateAppState。Authorized;}MainView。csMessenger。Default。Registerstring(this,OnUserNameMessage);fluentAPI。BindCommand(biLogout,xx。Logout());voidOnUserNameMessage(stringuserName){if(string。IsNullOrEmpty(userName))this。TextExpensesApplication;elsethis。TextExpensesApplication(userName);}
VB。NETLoginView。vbPasswordTextEdit。Properties。PasswordCharcMyDbContextViewModel。partial。vbprotectedvoidOnStateChanged()Me。RaiseCanExecuteChanged(Sub(x)x。Logout())IfStateAppState。AuthorizedThenMessenger。Default。Send(OfString)(loginViewModel。CurrentUser。Login)ElseMessenger。Default。Send(OfString)(String。Empty)EndIfpublicvoidLogout()StateAppState。ExitQueuedSystem。Diagnostics。Process。Start(System。Windows。Forms。Application。ExecutablePath)publicBooleanCanLogout()ReturnStateAppState。AuthorizedMainView。vbMessenger。Default。Register(OfString)(Me,AddressOfOnUserNameMessage)fluentAPI。BindCommand(biLogout,Function(x)x。Logout())voidOnUserNameMessage(StringuserName)IfString。IsNullOrEmpty(userName)ThenMe。TextExpensesApplicationElseMe。TextExpensesApplication(userName)EndIf
DevExpressWinForm
DevExpressWinForm拥有180组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!
祭出摩尔定律2。0指引封装方向,支撑台积电封装野心的技术有哪台积电的3DFabric先进封装平台由ChipStacking和AdvancedPackaging两部分组成,前者就是所谓的前道芯片堆叠技术,由CoW和WoW组成SoIC技术平……
家中联通光纤宽带200MB,测试速度才92。87mbps,需从故障的描述来看,测试速度为92。87Mbps,这是100兆的速度。由于联通宽带专家显示签约速度是200兆,这说明路由器或光猫存在网络传输瓶颈。按照网络技术标准,200兆……
美好的万圣节作文万圣节是西方国家的节日,传说在万圣节那天,会有鬼。所以很多人在家里制作了南瓜头,晚上来吓唬别人;还有小孩在别人家敲门说着:不给糖就捣乱;还有人用电子邮箱发贺卡来祝福万圣节……
iOS15正式版发布,升级还是停留?来看性能和续航测试苹果在今天凌晨向全体用户推送了以下系统更新:iOS15(19A346)iPadOS15(19A346)watchOS8(19R346)tvOS15(19……
白日梦这几天在看《穆斯林的葬礼》,感觉挺震撼的,一个穆斯林家族,六十年间的兴衰,三代人命运的沉浮,两个发生在不同时代、有着不同内容却又交错扭结的爱情悲剧。很优秀的一部长篇小说,有优美……
都说股市割韭菜,普通人能否在股市里实现稳定盈利?有人能稳定盈利,大部分人不能。凡是跟风的,都容易被割韭菜。有分析能力,长期坚持做一两支股票的,大部分收益可观。当你稳定盈利时,就不能算普通人了。这个是肯定可以的,要……
学会了X70系列多屏互动超级文档后,出差也能高效办公随着智能手机性能越来越高、屏幕越来越大、拍照越来越清晰,在日常生活中除了游戏、娱乐能给用户带来极致体验外,还能时办公时更加自如。vivoX70Pro(12GB512GB全……
股市十派一元,需要持股多久,一天可以吗?想要享受股票分红的权利,只要在股权登记日收盘前买入股票,当天晚上分红就会到账,等次日开盘就可以卖出,这样也是可以得到10派1元的分送。所以按此进行推测,想要享受到股票分红……
2017描写年夜饭的作文年夜饭是春节前的第一个节目,也是新年幸福的开始。yuwenmi小编整理了写年夜饭的作文范文,欢迎欣赏与借鉴。描写年夜饭的作文一:放寒假了,新年的脚步也近了。这次去爷爷、奶……
小狗狗罗拉作文我爷爷家养了一只狗他的名字叫罗拉。罗拉是拉布拉多名犬,它眼睛大大的忽闪忽闪真好看。眼睛上面有两个小黄点像它的眉毛。耳朵胖胖的向下耷拉着跑起路来直山呼。脖子上系着个红绳上面有一排……
关于快乐的春节作文600字春节到了,大街小巷热闹非凡,到处洋溢着节日的喜庆。以下是小编整理的关于快乐的春节作文600字,欢迎阅读。【篇一:快乐的春节】春节到了,到处都充满了快乐的气息。家家户户都贴……
比亚迪携北京鑫敏恒集团新能源专场试驾会第一季即将开启!GO车狂欢买车还在东奔西走?到处比价?来鑫敏恒瑞鑫看车,一站搞定!车型全、应有尽有真福利!礼所当燃!周末没时间看车?更不用怕6月4日,比亚迪……
荣耀Magic3至臻版更新新增支持微信3D人脸支付功能【手机中国新闻】在进入智能手机时代以来,我们的支付方式就发生了翻天覆地的变化。发展到现在,我们已经开始用指纹、面容完成支付行为,但是这样也会有一定的风险和限制。近日,手机中国发……
难忘军训的作文600字导语:军训生活已经过去5天了,但我每当走过操场便会想起那教官给我们示范的动作。下面是小编整理的一些军训优秀作文,欢迎查阅,谢谢!篇一:难忘的军训在漫长的三天军训中,让我感……
手机屏幕均衡党看过来聊聊手动最高亮度的重要性如果你对手机屏幕要求素质高,又追求功耗均衡、那么请花那么几分钟的时间、看看我来分析一下、对你择机会有用的。对手机屏幕有一定了解和关注的人都知道论显示效果和清晰度、ppi高……
最珍贵的礼物高一作文8篇【篇一:珍贵的礼物作文300字】在我过生日那天,许多人都给我送了生日礼物,而我的哥哥李欣浩却给我送了一件值得让我开心的礼物。你猜,他送的是什么礼物?他带我去济南游乐园,让……
想换台手机,什么牌子的好?中华有为,大有可为!华为现在正处于困难时期,作为一名中国人,必须支持一下。我18年的荣耀8X,现在依旧坚挺,打算换换电池,增加下内存,坐等华为王者归来!按键的老年非智能机……
初三观看里约奥运会有感征文奥运,在让咱们非常好地了解奥运精力:在全民健身和大众体育开展的助推下,奥运会从宏大叙事中解放出来,变得让每一位一般人更可接触。下面是小编整理的观看里约奥运会有感征文,欢迎大家参……
大势所趋!未来十年这些职业将逐渐衰败,失业者又要忙着找工作了随着5G商用、人工智能、数字化产业的发展和智慧城市、智能家居、无人驾驶、无人超市的广泛大规模应用,很多的职业都将会被淘汰。以下列举的是未来10年很有可能消失的职业,提供给……
照片里的故事作文500字虽然有些事情过一段时间我们会忘掉,但是照片可以把我们过去的事情记录下来为我们留下了许多美好,难忘的回忆。小编收集了照片里的故事作文500字,欢迎阅读。第一篇:照片里的故事……
告别折叠焦虑三星GalaxyZFlip35G诠释外刚内柔中坚折叠屏手机问世之初,虽然有着酷炫的外表,但却要承受着花瓶的质疑。因为真实存在的技术难题,让人们无法完全忽略折叠屏的耐用性。三星电子作为折叠屏发展的开拓者,始终致力于攻克一个个难……
如何选择家庭打印机?吞金兽要读幼儿园了,要打印的东西多了起来,买了一台家用打印机。买之前特意查了一下打印机的相关知识,记录下来供大家参考。一、打印机的分类打印机常见的分为喷墨打印机和激……
与母亲相处的日子作文800字窗外花开自无言,正是人间四月天,眼前尤现她的容颜。与母亲相处的日子,平淡,却又格外温暖、幸福。正值百花争艳的季节,繁花如锦幛,异常美丽,看惯了花开花落的我,也不由得……
大自然的叮咛作文600字淡淡的白云。蓝蓝天空青翠的树木,宽广的海洋,这一幅幅美丽的画面都相互呼应着,相互拼接,如拼图一般组成一副彩色的自然风景画,这就是大自然。随着人类的科技发展,自然渐渐被世人……