没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:龚雪|2021-12-07 09:40:39.640|阅读 301 次
概述:DevExpress WinForm创建的应用程序可利用MVVM设计模式,本文主要介绍命令功能,欢迎下载最新版体验!
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
获取工具下载 - DevExpress WinForm v21.2
DevExpress MVVM框架接受public void方法的一个参数作为参数化命令,您可以使用此参数在 View 和 ViewModel 之间传递数据。
C#
//ViewModel
public class ViewModelWithParametrizedCommand {
public void DoSomething(object p) {
var msgBoxService = this.GetService<IMessageBoxService>();
msgBoxService.ShowMessage(string.Format("The parameter is {0}.", p));
}
}
//View
mvvmContext.ViewModelType = typeof(ViewModelWithParametrizedCommand);
var fluent = mvvmContext.OfType<ViewModelWithParametrizedCommand>();
object parameter = 5;
fluent.BindCommand(commandButton, x => x.DoSomething, x => parameter);
VB.NET
'ViewModel
Public Class ViewModelWithParametrizedCommand
Public Sub DoSomething(ByVal p As Object)
Dim msgBoxService = Me.GetService(Of IMessageBoxService)()
msgBoxService.ShowMessage(String.Format("The parameter is {0}.", p))
End Sub
End Class
'View
mvvmContext.ViewModelType = GetType(ViewModelWithParametrizedCommand)
Dim fluent = mvvmContext.OfType(Of ViewModelWithParametrizedCommand)()
Dim parameter As Object = 5
fluent.BindCommand(commandButton, Sub(x) x.DoSomething(Nothing), Function(x) parameter)
您还可以向 CanExecute 条件添加参数。
C#
//ViewModel
public class ViewModelWithParametrizedConditionalCommand {
public void DoSomething(int p) {
var msgBoxService = this.GetService<IMessageBoxService>();
msgBoxService.ShowMessage(string.Format(
"The parameter is {0}.", p));
}
public bool CanDoSomething(int p) {
return (2 + 2) == p;
}
}
//View
mvvmContext.ViewModelType = typeof(ViewModelWithParametrizedConditionalCommand);
var fluent = mvvmContext.OfType<ViewModelWithParametrizedConditionalCommand>();
int parameter = 4;
fluent.BindCommand(commandButton, x => x.DoSomething, x => parameter);
VB.NET
'ViewModel
Public Class ViewModelWithParametrizedConditionalCommand
Public Sub DoSomething(ByVal p As Integer)
Dim msgBoxService = Me.GetService(Of IMessageBoxService)()
msgBoxService.ShowMessage(String.Format("The parameter is {0}.", p))
End Sub
Public Function CanDoSomething(ByVal p As Integer) As Boolean
Return (2 + 2) = p
End Function
End Class
'View
mvvmContext.ViewModelType = GetType(ViewModelWithParametrizedConditionalCommand)
Dim fluent = mvvmContext.OfType(Of ViewModelWithParametrizedConditionalCommand)()
Dim parameter As Integer = 4
fluent.BindCommand(commandButton, Sub(x) x.DoSomething(Nothing), Function(x) parameter)
如果需要执行延迟或连续操作,请使用异步命令。 要创建异步命令,请声明 System.Threading.Tasks.Task 类型的公共方法(您可以使用 async/await 语法),将 UI 元素绑定到命令的代码保持不变,框架在命令运行时禁用此元素。
C#
//ViewModel
public class ViewModelWithAsyncCommand {
public async Task DoSomethingAsync() {
// do some work here
await Task.Delay(1000);
}
}
//View
mvvmContext.ViewModelType = typeof(ViewModelWithAsyncCommand);
var fluent = mvvmContext.OfType<ViewModelWithAsyncCommand>();
fluent.BindCommand(commandButton, x => x.DoSomethingAsync);
VB.NET
'ViewModel Public Class ViewModelWithAsyncCommand Public Async Sub DoSomethingAsync() As Task ' do some work here Await Task.Delay(1000) End Sub End Class 'View mvvmContext.ViewModelType = GetType(ViewModelWithAsyncCommand) Dim fluent = mvvmContext.OfType(Of ViewModelWithAsyncCommand)() fluent.BindCommand(commandButton, Sub(x) x.DoSomethingAsync(Nothing))
任务支持取消标记,允许您检查 IsCancellationRequested 属性并在此属性返回 true 时中止任务。 如果将此代码添加到异步命令中,请使用 BindCancelCommand 方法创建停止正在进行的异步命令的 UI 元素。 DevExpress MVVM 框架锁定了这个取消按钮,并且只有在相关的异步命令正在运行时才启用它。
C#
//ViewModel
public class ViewModelWithAsyncCommandAndCancellation {
public async Task DoSomethingAsynchronously() {
var dispatcher = this.GetService<IDispatcherService>();
var asyncCommand = this.GetAsyncCommand(x => x.DoSomethingAsynchronously());
for(int i = 0; i <= 100; i++) {
if(asyncCommand.IsCancellationRequested)
break;
// do some work here
await Task.Delay(25);
await UpdateProgressOnUIThread(dispatcher, i);
}
await UpdateProgressOnUIThread(dispatcher, 0);
}
public int Progress {
get;
private set;
}
//update the "Progress" property bound to the progress bar within a View
async Task UpdateProgressOnUIThread(IDispatcherService dispatcher, int progress) {
await dispatcher.BeginInvoke(() => {
Progress = progress;
this.RaisePropertyChanged(x => x.Progress);
});
}
}
//View
mvvmContext.ViewModelType = typeof(ViewModelWithAsyncCommandAndCancellation);
var fluent = mvvmContext.OfType<ViewModelWithAsyncCommandAndCancellation>();
fluent.BindCommand(commandButton, x => x.DoSomethingAsynchronously);
fluent.BindCancelCommand(cancelButton, x => x.DoSomethingAsynchronously);
fluent.SetBinding(progressBar, p => p.EditValue, x => x.Progress);
VB.NET
'ViewModel Public Class ViewModelWithAsyncCommandAndCancellation Public Async Sub DoSomethingAsynchronously() As Task Dim dispatcher = Me.GetService(Of IDispatcherService)() Dim asyncCommand = Me.GetAsyncCommand(Sub(x) x.DoSomethingAsynchronously()) For i As Integer = 0 To 100 If asyncCommand.IsCancellationRequested Then Exit For End If ' do some work here Await Task.Delay(25) Await UpdateProgressOnUIThread(dispatcher, i) Next i Await UpdateProgressOnUIThread(dispatcher, 0) End Sub Private privateProgress As Integer Public Property Progress() As Integer Get Return privateProgress End Get Private Set(ByVal value As Integer) privateProgress = value End Set End Property 'update the "Progress" property bound to the progress bar within a View Private Async Sub UpdateProgressOnUIThread(ByVal dispatcher As IDispatcherService, ByVal progress As Integer) As Task Await dispatcher.BeginInvoke(Sub() Me.Progress = progress Me.RaisePropertyChanged(Sub(x) x.Progress) End Sub) End Sub End Class 'View mvvmContext.ViewModelType = GetType(ViewModelWithAsyncCommandAndCancellation) Dim fluent = mvvmContext.OfType(Of ViewModelWithAsyncCommandAndCancellation)() fluent.BindCommand(commandButton, Sub(x) x.DoSomethingAsynchronously) fluent.BindCancelCommand(cancelButton, Sub(x) x.DoSomethingAsynchronously) fluent.SetBinding(progressBar, Sub(p) p.EditValue, Sub(x) x.Progress)
WithCommand Fluent API 方法还支持可取消的异步命令。
C#
mvvmContext.ViewModelType = typeof(ViewModelWithAsyncCommandAndCancellation); // Initialize the Fluent API var fluent = mvvmContext.OfType<ViewModelWithAsyncCommandAndCancellation>(); // Binding for buttons fluent.WithCommand(x => x.DoSomethingAsynchronously) .Bind(commandButton) .BindCancel(cancelButton);
VB.NET
mvvmContext.ViewModelType = GetType(ViewModelWithAsyncCommandAndCancellation) ' Initialize the Fluent API Dim fluent = mvvmContext.OfType(Of ViewModelWithAsyncCommandAndCancellation)() ' Binding for buttons fluent.WithCommand(Sub(x) x.DoSomethingAsynchronously).Bind(commandButton).BindCancel(cancelButton)
DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!
更多产品正版授权详情及优惠,欢迎咨询
DevExpress技术交流群5:742234706 欢迎一起进群讨论
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn
文章转载自:慧都网



注意: Cogent DataHub 软件 v11 包含一些新功能,您的目标操作系统可能不支持这些功能。
本教程主要为大家介绍如何使用DevExpress WinForms数据网格控件进行数据排序的基础知识,欢迎下载最新版组件体验!
在使用Parasoft C/C++test执行BugDetective数据流分析时,可能会遇到用户自定义的资源API,那在这种情况下,若要判断是否存在资源问题,如资源泄露等,则需要手动配置测试配置。
大型SaaS系统的自动化测试常常受制于界面变化快、结构复杂、加载机制多变等因素。从元素识别到脚本管理,SmartBear TestComplete帮助Salesforce建了可靠的自动化测试体系。
相关产品
优秀的界面控件开发包,帮助企业构建卓越应用!
DevExpress DXperience Subscription高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!
DevExpress WinForms Subscription为Windows Forms平台创建具有影响力的业务解决方案,高性价比WinForms界面控件套包。
最新文章 MORE
半岛外围网上直营相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营 