翻譯|使用教程|編輯:龔雪|2021-12-01 09:41:41.197|閱讀 295 次
概述:DevExpress WinForm創(chuàng)建的應(yīng)用程序可利用MVVM設(shè)計(jì)模式,本文主要介紹命令功能,歡迎下載最新版體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
獲取工具下載 - DevExpress WinForm v21.2
在標(biāo)準(zhǔn)的 WinForms 應(yīng)用程序中,操作通常在事件處理程序中執(zhí)行。 例如,要在用戶單擊按鈕時(shí)刷新數(shù)據(jù),您需要處理 ButtonClick 事件并檢索數(shù)據(jù)源記錄。
這種標(biāo)準(zhǔn)技術(shù)不符合分離層的 MVVM 概念,從數(shù)據(jù)源中提取數(shù)據(jù)的代碼應(yīng)該屬于 ViewModel 層,而不是 View。 在 MVVM 中,這些任務(wù)是通過命令來完成的——封裝了操作的 ViewModel 對象。將一個 UI 元素綁定到該對象,以實(shí)現(xiàn)所需的層分離:View 代碼現(xiàn)在只有綁定代碼,而所有業(yè)務(wù)邏輯都保留在 ViewModel 中,可以安全地更改。
DevExpress MVVM 框架將所有公共無效方法視為可綁定命令,下面的代碼說明了如何聲明一個使用服務(wù)來顯示消息框的命令。您可以通過以下鏈接在 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”結(jié)尾的方法會引發(fā)異常 - 重命名此類方法或使用 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 方法允許您同時(shí)綁定多個按鈕。
運(yùn)行演示: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)
要指定確定命令是否應(yīng)該運(yùn)行的條件,請聲明一個布爾方法,其名稱以“Can”開頭,后跟相關(guān)命令名稱,這些方法稱為 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 元素的狀態(tài)(禁用、取消選中或隱藏此元素)。 上面的代碼示例來自以下演示:Command with CanExecute condition,運(yùn)行此演示并更改條件,使其始終返回 false。“Execute Command”按鈕變?yōu)榻茫驗(yàn)槠湎嚓P(guān)命令無法再運(yùn)行。
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平臺創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
更多產(chǎn)品正版授權(quán)詳情及優(yōu)惠,歡迎咨詢
DevExpress技術(shù)交流群5:742234706 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)