約定和屬性
立即下載DevExpress WinForms
MVVM框架處理您的應(yīng)用程序代碼,并用自己的方式解釋特定的代碼片段,例如如果語法正確,屬性可以被認為是可綁定的,這些語法規(guī)則稱為約定。約定可以避免編寫額外的代碼,因為框架將“understand”您的期望,并自動生成所需的一切。本文檔收集了在構(gòu)建MVVM應(yīng)用程序時需要了解的所有MVVM框架約定。
可綁定屬性
- 所有公共的虛擬屬性都被視為可綁定的。
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屬性標記這些屬性,以便能夠?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īng),請聲明屬性依賴項。屬性依賴是一種方法,當其相關(guān)屬性發(fā)生更改或即將更改時,該方法會自動執(zhí)行。要實現(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. Changed方法也可以有一個參數(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ù)的公共void方法都被視為。
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(false)屬性修飾一個void方法,告訴框架這不是一個有效的MVVM命令。
C#:
using DevExpress.Mvvm.DataAnnotations; [Command(false)] public void DoSomethingInternal(object p) { // TODO }
VB.NET:
Imports DevExpress.Mvvm.DataAnnotations <Command(false)> Public Sub DoSomethingInternal(ByVal p As Object) ' TODO End Sub
- 名稱以…Command結(jié)尾的方法將引發(fā)異常,通過使用Command屬性標記這些方法并通過Name參數(shù)設(shè)置適當?shù)拿Q,您可以強制框架將這些方法視為有效的命令。
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)的支持屬性,默認情況下此屬性會用相關(guān)方法加上“Command”后綴命名,您可以使用Command屬性的Name參數(shù)為這個自動生成的支持屬性保留另一個名稱。
C#:
[Command(Name = "MyMvvmCommand")] public void DoSomething(object p) { //do something }
VB.NET:
<Command(Name := "MyMvvmCommand")> Public Sub DoSomething(ByVal p As Object) 'do something End Sub
- 命令可以伴隨著CanExecute子句—— boolean methods,它只允許在返回true時執(zhí)行相關(guān)命令,這樣的方法必須調(diào)用Can<Related_Command_Name>。
C#:
//this command will be executed only if "p" equals 4 public void DoSomething(int p) { MessageBox.Show(string.Format("The parameter passed to command is {0}.", p)); } public bool CanDoSomething(int p) { return (2 + 2) == p; }
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
當命令剛剛綁定到目標時,首先檢查CanExecute子句(以獲取目標的初始狀態(tài))次CanExecuteChanged事件通知命令的目標關(guān)于命令狀態(tài)更改時,都會重新計算該標準。此事件在底層命令對象級別聲明,要從ViewModel級別發(fā)送這樣的通知,請調(diào)用RaiseCanExecuteChanged擴展方法,如下所示。
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
Services
- 為了解析服務(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屬性顯式地用其他名稱標記服務(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
- 當框架覆蓋一個服務(wù)屬性時,它會生成相應(yīng)的GetService<>擴展方法調(diào)用,ServiceProperty屬性允許您為該方法指定其他參數(shù)。
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