翻譯|使用教程|編輯:龔雪|2021-12-07 09:40:39.640|閱讀 291 次
概述:DevExpress WinForm創(chuàng)建的應用程序可利用MVVM設計模式,本文主要介紹命令功能,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
獲取工具下載 - DevExpress WinForm v21.2
DevExpress MVVM框架接受public void方法的一個參數(shù)作為參數(shù)化命令,您可以使用此參數(shù)在 View 和 ViewModel 之間傳遞數(shù)據(jù)。
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 條件添加參數(shù)。
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)
如果需要執(zhí)行延遲或連續(xù)操作,請使用異步命令。 要創(chuàng)建異步命令,請聲明 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 方法創(chuàng)建停止正在進行的異步命令的 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平臺創(chuàng)建具有影響力的業(yè)務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業(yè)務數(shù)據(jù),它都能輕松勝任!
更多產品正版授權詳情及優(yōu)惠,歡迎咨詢
DevExpress技術交流群5:742234706 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網(wǎng)