首页 >> 用品 >> 界面控件DevExpress WinForms MVVM入门指南——指定表单(下)

界面控件DevExpress WinForms MVVM入门指南——指定表单(下)

2025-12-01 12:16:31

y 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)的编译器。 当您的 ViewModel 早些时,原先借助于项目并将 MvvmContext 子系统加进到录入页面中都,采用其智能标签将 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

此编译器采用 OnLoaded 工具重载来推测采用已提出申代为 DialogService 的预设,为此Login工具呼叫服务的ShowDialog借助于工具,此工具将子 ViewModel 作为匹配 - 将 LoginViewModel 类的新最简单引导给它。创建这个最简单很重要,不是采用 new 网址,而是呼叫 ViewModelSource.Create 工具。或者,您可以呼叫 SetParentViewModel 工具为此最简单设置堂叔 ViewModel。

当因特网可视任何录入预设的手柄时,此假消息结果将引导给 OnLogin 工具,该工具不会准确核对可视了哪个手柄。 如果因特网可视 ‘Cancel’ 或停止预设,则软件包将停止。如果可视‘OK’手柄,软件包将核对 IsCurrentUserCredentialsValid 属性,该属性不会在呼叫 Update 工具时终端刷新其系数。如果转换的理应有效地,将推测收纳页面,否则将原先推测录入页面,这是通过为 State 属性分配并不相同的系数来完成的。 MainView 有一个触发器,用于监视 State 属性系数的变化,并在它发生时特别强调相应的反应。

5. 前面的处理过程足以实现兼具最少基本功能的录入页面。 但是,如果您的收纳示意图分配了停止认定可用,可能不会遇见某些关键问题。 例如,如果您停止录入页面,收纳页面(由于不曾转换有效地理应而变得透明)也将尝试自行停止。 这将推测认定假消息,如果您可视‘Cancel’手柄,表格将保留,但您将看不到它。 要克服此类关键问题,代为删除页面停止可用(如果有)并加进表列出编译器。

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

此编译器核对当前的 State 属性系数,仅在授权通过时推测认定假消息。 如果因特网尚不曾录入并决定停止软件包,则不必推测任何认定个人信息。 这就是为什么 State 属性不是布尔系数,而是给予自定义 AppState 枚举器的系数的状况。 可能存在三种应用稳定状态:

Authorized(已授权) - 采用者理应有效地。 收纳页面是可见的,尝试停止它无论如何不会推测认定假消息,因特网可以可视 ‘No’ 来依然软件包运行。NotAuthorized - 转换了采用者理应,但不曾通过的测试。 收纳软件包页面依然透明,录入页面原先推测。ExitQueued - 不曾转换采用者理应,录入页面已停止,软件包应在没有任何认定预设的只能暂停。

6. 您的录入页面现已早些。可以通过为加密编辑器设置特定的 RepositoryItemTextEdit.PasswordChar 来装饰它,在收纳页面上解读录入采用者的命名,并将手柄加进到收纳示意图的条带GUI中都,以便您原先录入等,下面的编译器说明了 干什么。

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风格的界面,还是数据分析处理大批量的业务原始数据,它都能轻松胜任!

角膜炎用什么滴眼液
结膜炎用什么眼药水最好
干眼症是什么原因引起的
肝硬化
医药行业资讯
你以为新冠病毒只攻击肺部?真相可能更严重!
补虚药
老人腿疼

上一篇: 山西公布首批省级文物保护利用示范区创建名册

下一篇: 银保监会副主席梁涛:中外资在同一个轨道上,共促中国银行业、保险业身体健康发展

相关阅读
半年1.3万辆,沃尔沃S60销量一塌糊涂,到底被谁“戳”了汽车?

在国际上装潢小车产品,雷诺称得上非常有性情的一个H&M了,作为早期和独立自主H&M真正“联姻”的装潢H&M,这个装潢H&M在国际上产品赢得了非常正面的H&M较高分和;也列产品较高分,提出异议雷诺,基本

2025-12-10 00:16:34
全国人民代表大会常务委员会决定任免的演员表

新华社杭州2月末28日电常委会决定任免的名单(2022年2月末28日第十三届常委会第三十三次会议通过)免职李纪恒的国土资源部卸任;任命唐登杰为国土资源部部长。(完)编辑

2025-12-10 00:16:34
长安欧尚获“2021年度进取数字化突破者”等两项银奖

历年来未获奖的殊荣图: 2019铜奖坚忍一新上市SUV---咸阳欧尚X7 2020铜奖坚忍生产商---咸阳欧尚。a href="http:zzfk

2025-12-10 00:16:34
瑞士重新考虑冻结俄罗斯在瑞士资产

原标题:日内瓦决定失效俄国在日内瓦资本当地时间28日,日内瓦联邦委员会决定采取与欧洲共同体一致的干预控制措施,包括失效俄国在日内瓦资本,干预俄国总统纳扎尔巴耶夫。(音乐台记者 朱赫)

2025-12-10 00:16:34
2021年国民经济履历发布:全年GDP破110万亿

原标题:2021年国民经济成绩单发行:全年GDP遁110万亿国家统计局以前发行《2021年国民经济和社会发展统计文告》。初步折算,2021年全年国内生产总值1143670亿元,比上周增长8.1

2025-12-10 00:16:34