• <menu id="w2i4a"></menu>
  • logo DevExpress WinForm中文手冊(cè)

    約定和屬性


    立即下載DevExpress WinForms

    MVVM框架處理您的應(yīng)用程序代碼,并用自己的方式解釋特定的代碼片段,例如如果語法正確,屬性可以被認(rèn)為是可綁定的,這些語法規(guī)則稱為約定。約定可以避免編寫額外的代碼,因?yàn)榭蚣軐ⅰ皍nderstand”您的期望,并自動(dòng)生成所需的一切。本文檔收集了在構(gòu)建MVVM應(yīng)用程序時(shí)需要了解的所有MVVM框架約定。

    可綁定屬性

    C#:

    public virtual string Test { get; set; }

    VB.NET:

    Public Overridable Property Test() As String

    • 要抑制此類屬性的可綁定屬性生成,請(qǐng)使用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)行時(shí)更改其值,要跟蹤這些更改并對(duì)其作出響應(yīng),請(qǐng)聲明屬性依賴項(xiàng)。屬性依賴是一種方法,當(dāng)其相關(guān)屬性發(fā)生更改或即將更改時(shí),該方法會(huì)自動(dòng)執(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. Changed方法也可以有一個(gè)參數(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中聲明的所有帶有零或一個(gè)參數(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)屬性修飾一個(gè)void方法,告訴框架這不是一個(gè)有效的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屬性標(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
    • 對(duì)于每個(gè)命令方法,框架都會(huì)生成相應(yīng)的支持屬性,默認(rèn)情況下此屬性會(huì)用相關(guān)方法加上“Command”后綴命名,您可以使用Command屬性的Name參數(shù)為這個(gè)自動(dòng)生成的支持屬性保留另一個(gè)名稱。

    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時(shí)執(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

    當(dāng)命令剛剛綁定到目標(biāo)時(shí),首先檢查CanExecute子句(以獲取目標(biāo)的初始狀態(tài))次CanExecuteChanged事件通知命令的目標(biāo)關(guān)于命令狀態(tài)更改時(shí),都會(huì)重新計(jì)算該標(biāo)準(zhǔn)。此事件在底層命令對(duì)象級(jí)別聲明,要從ViewModel級(jí)別發(fā)送這樣的通知,請(qǐng)調(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

    Services

    • 為了解析服務(wù)框架會(huì)覆蓋接口類型的虛擬屬性,接口名稱必須用…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)框架覆蓋一個(gè)服務(wù)屬性時(shí),它會(huì)生成相應(yīng)的GetService<>擴(kuò)展方法調(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
    掃碼咨詢


    添加微信 立即咨詢

    電話咨詢

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();