DevExpress WinForm控件入门指南:WinForms MVVM – 命令(一)

DevExpress WinForm创建的应用程序可利用MVVM设计模式,本文主要介绍命令功能,欢迎下载最新版体验!

在标准的 WinForms 应用程序中,操作通常在事件处理程序中执行。 例如,要在用户单击按钮时刷新数据,您需要处理 ButtonClick 事件并检索数据源记录。

这种标准技术不符合分离层的 MVVM 概念,从数据源中提取数据的代码应该属于 ViewModel 层,而不是 View。 在 MVVM 中,这些任务是通过命令来完成的——封装了操作的 ViewModel 对象。将一个 UI 元素绑定到该对象,以实现所需的层分离:View 代码现在只有绑定代码,而所有业务逻辑都保留在 ViewModel 中,可以安全地更改。

DevExpress MVVM 框架将所有公共无效方法视为可绑定命令,下面的代码说明了如何声明一个使用服务来显示消息框的命令。您可以通过以下链接在 DevExpress 演示中心查看完整演示:Simple Command。

C#

//POCO ViewModelpublic class ViewModelWithSimpleCommand {//commandpublic void DoSomething() {var msgBoxService = this.GetService<IMessageBoxService>();msgBoxService.ShowMessage("Hello!");}}

VB.NET

'POCO ViewModelPublic Class ViewModelWithSimpleCommand'commandPublic Sub DoSomething()Dim msgBoxService = Me.GetService(Of IMessageBoxService)()msgBoxService.ShowMessage("Hello!")End SubEnd Class

重要提示:名称以“Command”结尾的方法会引发异常 – 重命名此类方法或使用 Command 属性修饰它们。

要将按钮链接到此命令,请使用 BindCommand 或 WithCommand 方法。

C#

//View codemvvmContext.ViewModelType = typeof(ViewModelWithSimpleCommand);var fluent = mvvmContext.OfType<ViewModelWithSimpleCommand>();fluent.BindCommand(commandButton, x => x.DoSomething);\orfluent.WithCommand(x => x.DoSomething).Bind(commandButton1);

VB.NET

'View codemvvmContext.ViewModelType = GetType(ViewModelWithSimpleCommand)Dim fluent = mvvmContext.OfType(Of ViewModelWithSimpleCommand)()fluent.BindCommand(commandButton, Sub(x) x.DoSomething)'orfluent.WithCommand(Sub(x) x.DoSomething).Bind(commandButton1)

WithCommand 方法允许您同时绑定多个按钮。

运行演示:Bind to Multiple UI Elements.

C#

//Viewvar fluent = mvvmContext.OfType<ViewModelWithSimpleCommand>();fluent.WithCommand(x => x.DoSomething).Bind(commandButton1).Bind(commandButton2);

VB.NET

'ViewDim fluent = mvvmContext.OfType(Of ViewModelWithSimpleCommand)()fluent.WithCommand(Sub(x) x.DoSomething).Bind(commandButton1).Bind(commandButton2)
可以执行条件

要指定确定命令是否应该运行的条件,请声明一个布尔方法,其名称以“Can”开头,后跟相关命令名称,这些方法称为 CanExecute 条件。

C#

//ViewModelpublic class ViewModelWithConditionalCommand {//Commandpublic void DoSomething() {var msgBoxService = this.GetService<IMessageBoxService>();msgBoxService.ShowMessage("Hello!");}//CanExecute conditionpublic bool CanDoSomething() {return (2 + 2) == 4;}}

VB.NET

'ViewModelPublic Class ViewModelWithConditionalCommand'CommandPublic Sub DoSomething()Dim msgBoxService = Me.GetService(Of IMessageBoxService)()msgBoxService.ShowMessage("Hello!")End Sub'CanExecute conditionPublic Function CanDoSomething() As BooleanReturn (2 + 2) = 4End FunctionEnd Class

您还可以忽略 CanExecute 名称要求并使用 Command 属性手动分配命令条件。

C#

[Command(CanExecuteMethodName = "DoSomethingCriteria")]public void DoSomething(int p) {//command}

VB.NET

<Command(CanExecuteMethodName := "DoSomethingCriteria")>Public Sub DoSomething(ByVal p As Integer)'commandEnd Sub

如果 CanExecute 条件返回 false,则框架会更改链接到此命令的 UI 元素的状态(禁用、取消选中或隐藏此元素)。 上面的代码示例来自以下演示:Command with CanExecute condition,运行此演示并更改条件,使其始终返回 false。“Execute Command”按钮变为禁用,因为其相关命令无法再运行。

C#

//ViewModelpublic bool CanDoSomething() {//always "false"return (2 + 2) == 5;}

VB.NET

'ViewModelPublic Function CanDoSomething() As Boolean'always "False"Return (2 + 2) = 5End Function

在以下情况下,框架会检查 CanExecute 条件:

  • 一个 UI 命令绑定初始化;
  • 调用 RaiseCanExecuteChanged 方法。 在下面的示例中,每次 SelectedEntity 属性更改时,都会重新检查 CanDoSomething 条件的返回值。

C#

//Bindable Propertypublic virtual MyEntity SelectedEntity{ get; set; }//OnChanged callback for the bindable propertyprotected void OnSelectedEntityChanged(){this.RaiseCanExecuteChanged(x=>x.DoSomething());}//Commandpublic void DoSomething() {//. . .}//CanExecute conditionpublic bool CanDoSomething() {//. . .}

VB.NET

'Bindable PropertyPublic Overridable Property SelectedEntity() As MyEntity'OnChanged callback for the bindable propertyProtected Sub OnSelectedEntityChanged()Me.RaiseCanExecuteChanged(Function(x) x.DoSomething())End Sub'CommandPublic Sub DoSomething()'. . .End Sub'CanExecute conditionPublic Function CanDoSomething() As Boolean'. . .End Function

DevExpress WinForm | 下载试用

DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!

更多产品正版授权详情及优惠,欢迎咨询在线客服>>


DevExpress技术交流群5:742234706      欢迎一起进群讨论

更多DevExpress线上公开课、中文教程资讯请上中文网获取

DevExpress年终放大招!省钱攻略提前奉上,更适合中国区用户!
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年11月1日
下一篇 2021年11月1日

相关推荐

发表回复

登录后才能评论