2014-12-03 51 views
0

NotifyOfPropertyChange使用Caliburn.Micro,NotifyPropertyChange(從基類PropertyChangedBase的),因此Caliburn.Micro在VB

NotifyOfPropertyChange(() => MyPropertyName) 

證明在哪裏MyPropertyName是,從邏輯上講,某種形式的財產。我不清楚這是如何工作的,但我猜想,因爲返回屬性的匿名函數是作爲參數給出的,所以CM可以做一些反射魔術來找到實際的屬性名稱。比將「MyPropertyName」作爲字符串傳遞更方便,因爲這是錯字傾向。

我的問題是,我如何在VB.Net中使用它?直譯是

NotifyOfPropertyChange(Function() MyPropertyName) 

但是,這給了我

Cannot convert lambda expression to type 'string' because it is not a delegate type. 

出現在C#中類似的錯誤時MyPropertyName實際上不是一個屬性,但似乎總是出現在VB。

這可以在VB中完成嗎?

回答

0

不是一個實際的答案,但我已經找到了一個變通感謝this answer on another question: 通過實施不接受委託的擴展方法,我已經能夠使用NotifyOfPropertyChange沒有通過字符串文字:

(進口System.Linq.Expressions以及System.Runtime.CompilerServices :)

<Extension> 
Public Sub NotifyOfPropertyChange(Of T)(handler As PropertyChangedBase, propertyExpression As Expression(Of Func(Of T))) 
    If handler IsNot Nothing Then 
     Dim body As MemberExpression = TryCast(propertyExpression.Body, MemberExpression) 
     If body Is Nothing Then Throw New ArgumentException("'propertyExpression' should be a member expression") 

     Dim expression As ConstantExpression = TryCast(body.Expression, ConstantExpression) 
     If expression Is Nothing Then Throw New ArgumentException("'propertyExpression' body should be a constant expression") 

     Dim target = Linq.Expressions.Expression.Lambda(expression).Compile().DynamicInvoke 

     handler.NotifyOfPropertyChange(body.Member.Name) 
    End If 
End Sub 

我這才得以使用

NotifyOfPropertyChange(Function() MyPropertyName)