没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:龚雪|2021-12-07 09:40:39.640|阅读 222 次
概述: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 欢迎一起进群讨论
更多DevExpress线上公开课、中文教程资讯请上中文网获取
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:慧都网本文主要介绍如何使用DevExpress WPF Grid控件实现节点(Nodes)的遍历,欢迎下载最新版组件体验!
AG Grid 是一个与框架无关的数据网格,它为 React、Angular、Vue 和 Vanilla JS 提供官方支持,本文将重点介绍一些可以使用 AG Grid 添加到应用程序的特性和功能的示例,以及现场演示和示例代码。
从 2025.2 版本开始,用于仪表板创建的 Stimulsoft 产品引入了InclusionMode属性,我们将在本文中对其进行探讨。
本文将为大家介绍如何在Telerik UI for WinForms应用中使用Kendo UI for Angular组件来交换通信和事件,欢迎下载新版组件体验!
优秀的界面控件开发包,帮助企业构建卓越应用!
DevExpress DXperience Subscription高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!
DevExpress WinForms Subscription为Windows Forms平台创建具有影响力的业务解决方案,高性价比WinForms界面控件套包。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号