其他分享
首页 > 其他分享> > 界面控件DevExpress WinForm - MVVM命令讲解(一)

界面控件DevExpress WinForm - MVVM命令讲解(一)

作者:互联网

获取工具下载 - DevExpress WinForm v21.2

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

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

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

C#

 

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

 

VB.NET

 

'POCO ViewModel
Public Class ViewModelWithSimpleCommand
'command
Public Sub DoSomething()
Dim msgBoxService = Me.GetService(Of IMessageBoxService)()
msgBoxService.ShowMessage("Hello!")
End Sub
End Class

 

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

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

C#

 

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

 

VB.NET

 

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

 

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

运行演示:Bind to Multiple UI Elements.

C#

 

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

 

VB.NET

 

'View
Dim fluent = mvvmContext.OfType(Of ViewModelWithSimpleCommand)()
fluent.WithCommand(Sub(x) x.DoSomething)
.Bind(commandButton1)
.Bind(commandButton2)

 

可以执行条件

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

C#

 

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

 

VB.NET

 

'ViewModel
Public Class ViewModelWithConditionalCommand
'Command
Public Sub DoSomething()
Dim msgBoxService = Me.GetService(Of IMessageBoxService)()
msgBoxService.ShowMessage("Hello!")
End Sub
'CanExecute condition
Public Function CanDoSomething() As Boolean
Return (2 + 2) = 4
End Function
End 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)
'command
End Sub

 

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

C#

 

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

 

VB.NET

 

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

 

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

C#

 

//Bindable Property
public virtual MyEntity SelectedEntity{ get; set; }

//OnChanged callback for the bindable property
protected void OnSelectedEntityChanged(){
this.RaiseCanExecuteChanged(x=>x.DoSomething());
}

//Command
public void DoSomething() {
//. . .
}

//CanExecute condition
public bool CanDoSomething() {
//. . .
}

 

VB.NET

 

'Bindable Property
Public Overridable Property SelectedEntity() As MyEntity

'OnChanged callback for the bindable property
Protected Sub OnSelectedEntityChanged()
Me.RaiseCanExecuteChanged(Function(x) x.DoSomething())
End Sub

'Command
Public Sub DoSomething()
'. . .
End Sub

'CanExecute condition
Public Function CanDoSomething() As Boolean
'. . .
End Function

 

DevExpress WinForm | 下载试用

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


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

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

 

标签:控件,End,Sub,DoSomething,DevExpress,public,Command,Public,MVVM
来源: https://www.cnblogs.com/AABBbaby/p/15631762.html