翻譯|使用教程|編輯:龔雪|2021-12-01 09:41:41.197|閱讀 295 次
概述:DevExpress WinForm創建的應用程序可利用MVVM設計模式,本文主要介紹命令功能,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
獲取工具下載 - 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擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
更多產品正版授權詳情及優惠,歡迎咨詢
DevExpress技術交流群5:742234706 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網