原創(chuàng)|使用教程|編輯:龔雪|2021-09-28 10:35:57.980|閱讀 249 次
概述:本文主要介紹如何DevExpress WinForms MVVM的慣例和屬性,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
MVVM框架以自己的方式處理您的應(yīng)用程序代碼并解釋特定的代碼片段,例如如果語法正確,則屬性可以被視為可綁定,這些語法規(guī)則稱為慣例。慣例允許您避免編寫額外的代碼,因為框架將“理解”您對它的期望并自動生成所需的一切。本文檔收集了您在構(gòu)建MVVM應(yīng)用程序時需要注意的所有MVVM框架慣例。
獲取工具下載 - DevExpress WinForm v21.1
所有公共自動實(shí)現(xiàn)的虛擬屬性都被視為可綁定的。
C#
public virtual string Test { get; set; }
VB.NET
Public Overridable Property Test() As String
要禁止為此類屬性生成可綁定屬性,請按如下方式使用 Bindable 屬性。
C#
[Bindable(false)] public virtual string Test { get; set; }
VB.NET
<Bindable(False)> Public Overridable Property Test() As String
框架會忽略帶有支持字段的屬性,您可以使用 BindableProperty 特性顯式標(biāo)記此類屬性,以便仍然能夠?qū)⑺鼈冇糜跀?shù)據(jù)綁定。
C#
using DevExpress.Mvvm.DataAnnotations; //. . . string test; [BindableProperty] public virtual string Test { get { return test; } set { test = value; } }
VB.NET
Imports DevExpress.Mvvm.DataAnnotations '. . . Private testField As String <BindableProperty> Public Overridable Property Test() As String Get Return testField End Get Set(ByVal value As String) testField = value End Set End Property
屬性可以在應(yīng)用程序運(yùn)行時更改其值,要跟蹤這些更改并對其做出響應(yīng),請聲明屬性依賴項。屬性依賴是一種在其相關(guān)屬性發(fā)生變化或即將發(fā)生變化時自動執(zhí)行的方法。 要實(shí)現(xiàn)此操作,必須調(diào)用On<Related_Property_Name>Changing或On<Related_Property_Name>Changed方法。
C#
public virtual string Test { get; set; } protected void OnTestChanged() { //do something }
VB.NET
Public Overridable Property Test() As String Protected Sub OnTestChanged() 'do something End Sub
On...Changed 和 On..Changing 方法也可以有一個參數(shù),在這種情況下,參數(shù)將分別接收舊的或新的屬性值。
C#
public virtual string Test { get; set; } protected void OnTestChanging(string newValue) { //do something } protected void OnTestChanged(string oldValue) { //do something }
VB.NET
Public Overridable Property Test() As String Protected Sub OnTestChanging(ByVal newValue As String) 'do something End Sub Protected Sub OnTestChanged(ByVal oldValue As String) 'do something End Sub
BindableProperty 屬性也允許您使用具有不同名稱的方法。
C#
[BindableProperty(OnPropertyChangingMethodName = "BeforeChange", OnPropertyChangedMethodName = "AfterChange")] public virtual string Test { get; set; } protected void BeforeChange() { //. . . } protected void AfterChange() { //. . . }
VB.NET
<BindableProperty(OnPropertyChangingMethodName := "BeforeChange", OnPropertyChangedMethodName := "AfterChange")> Public Overridable Property Test() As String Protected Sub BeforeChange() '. . . End Sub Protected Sub AfterChange() '. . . End Sub
在 POCO ViewModels 中聲明的所有具有零個或一個參數(shù)的公共無效方法都被視為命令。
C#
public void DoSomething(object p) { MessageBox.Show(string.Format("The parameter passed to command is {0}.", p)); }
VB.NET
Public Sub DoSomething(ByVal p As Object) MessageBox.Show(String.Format("The parameter passed to command is {0}.", p)) End Sub
名稱以 ...Command 結(jié)尾的方法會引發(fā)異常,您可以通過使用 Command 屬性標(biāo)記這些方法并通過 Name 參數(shù)設(shè)置適當(dāng)?shù)拿Q來強(qiáng)制框架將這些方法視為有效命令。
C#
using DevExpress.Mvvm.DataAnnotations; [Command(Name="DoSomething")] public void DoSomethingCommand(object p) { //do something }
VB.NET
Imports DevExpress.Mvvm.DataAnnotations <Command(Name := "DoSomething")> Public Sub DoSomethingCommand(ByVal p As Object) 'do something End Sub
對于每個命令方法,框架都會生成相應(yīng)的支持屬性。 默認(rèn)情況下,此屬性以相關(guān)方法加上“Command”后綴命名。 您可以使用 Command 屬性的 Name 參數(shù)為這個自動生成的支持屬性保留另一個名稱。
C#
[Command(Name = "MyMvvmCommand")] public void DoSomething(object p) { //do something }
VB.NET
'this command will be executed only if "p" equals 4 Public Sub DoSomething(ByVal p As Integer) MessageBox.Show(String.Format("The parameter passed to command is {0}.", p)) End Sub Public Function CanDoSomething(ByVal p As Integer) As Boolean Return (2 + 2) = p End Function
具有其他名稱的 CanExecute 方法仍然可以通過使用 Command 屬性的 CanExecuteMethodName 參數(shù)綁定到命令。
C#
[Command(CanExecuteMethodName = "DoSomethingCriteria")] public void DoSomething(int p) { MessageBox.Show(string.Format("The parameter passed to command is {0}.", p)); } public bool DoSomethingCriteria(int p) { return (2 + 2) == p; }
VB.NET
<Command(CanExecuteMethodName := "DoSomethingCriteria")> Public Sub DoSomething(ByVal p As Integer) MessageBox.Show(String.Format("The parameter passed to command is {0}.", p)) End Sub Public Function DoSomethingCriteria(ByVal p As Integer) As Boolean Return (2 + 2) = p End Function
當(dāng)命令剛剛綁定到它的目標(biāo)(以獲取目標(biāo)的初始狀態(tài))時,首先檢查 CanExecute 子句。 稍后,每次 CanExecuteChanged 事件通知命令的目標(biāo)有關(guān)命令狀態(tài)更改時,都會重新計算此條件。 此事件是在底層命令對象級別聲明的,要從 ViewModel 級別發(fā)送此類通知,請按如下方式調(diào)用 RaiseCanExecuteChanged 擴(kuò)展方法。
C#
//a bindable property public virtual bool IsModified { get; protected set; } //a command public void Save() { //. . . } //a CanExecute condition public bool CanSave() { return IsModified; } //the OnChanged method calls the RaiseCanExecuteChanged method for the "Save" command //this forces the command to update its CanExecute condition public void OnIsModifiedChanged() { this.RaiseCanExecuteChanged(x=>x.Save()); }
VB.NET
'a bindable property Private privateIsModified As Boolean Public Overridable Property IsModified() As Boolean Get Return privateIsModified End Get Protected Set(ByVal value As Boolean) privateIsModified = value End Set End Property 'a command Public Sub Save() '. . . End Sub 'a CanExecute condition Public Function CanSave() As Boolean Return IsModified End Function 'the OnChanged method calls the RaiseCanExecuteChanged method for the "Save" command 'this forces the command to update its CanExecute condition Public Sub OnIsModifiedChanged() Me.RaiseCanExecuteChanged(Sub(x) x.Save()) End Sub
為了解析服務(wù),框架會覆蓋接口類型的虛擬屬性,這些屬性的名稱必須以 ...Service 結(jié)尾。
C#
public virtual IMyNotificationService MyService { get { throw new NotImplementedException(); } } public virtual IMyNotificationService AnotherService { get { throw new NotImplementedException(); } }
VB.NET
Public Overridable ReadOnly Property MyService() As IMyNotificationService Get Throw New NotImplementedException() End Get End Property Public Overridable ReadOnly Property AnotherService() As IMyNotificationService Get Throw New NotImplementedException() End Get End Property
您還可以使用 ServiceProperty 屬性顯式標(biāo)記具有其他名稱的服務(wù)屬性。
C#
using DevExpress.Mvvm.DataAnnotations; //. . . [ServiceProperty] public virtual IMyNotificationService MyProvider { get { throw new NotImplementedException(); } }
VB.NET
Imports DevExpress.Mvvm.DataAnnotations '. . . <ServiceProperty> Public Overridable ReadOnly Property MyProvider() As IMyNotificationService Get Throw New NotImplementedException() End Get End Property
當(dāng)框架覆蓋服務(wù)屬性時,它會生成相應(yīng)的 GetService<> 擴(kuò)展方法調(diào)用。 ServiceProperty 屬性允許您為此方法指定其他參數(shù)(例如,服務(wù)密鑰)。
C#
[ServiceProperty(Key="Service1")] public virtual IMyNotificationService Service { get { throw new NotImplementedException(); } } [ServiceProperty(Key = "Service2")] public virtual IMyNotificationService AnotherService { get { throw new NotImplementedException(); } }
VB.NET
<ServiceProperty(Key:="Service1")> Public Overridable ReadOnly Property Service() As IMyNotificationService Get Throw New NotImplementedException() End Get End Property <ServiceProperty(Key := "Service2")> Public Overridable ReadOnly Property AnotherService() As IMyNotificationService Get Throw New NotImplementedException() End Get End Property
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ù)交流群4:715863792 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)