2010-07-19 202 views
0

我在C#中有一段代碼,但無法轉換爲VB.NET。我試過在線轉換器,但VS2008總是給編譯錯誤。任何幫助是極大的讚賞。VB.NET等價於C#代碼

foreach (Binding knownBinding in allKnownBindings) 
{ 
    string errorMessage = ((IDataErrorInfo)this.DataContext)[knownBinding.Path.Path]; 
    if (errorMessage != null && errorMessage.Length > 0) 
    { 
     isValid = false; 

     // Display the error on any elements bound to the property 
     FindBindingsRecursively(
     this.Parent, 
     delegate(FrameworkElement element, Binding binding, DependencyProperty dp) 
     { 
      if (knownBinding.Path.Path == binding.Path.Path) 
      { 

       BindingExpression expression = element.GetBindingExpression(dp); 
       ValidationError error = new ValidationError(
         new ExceptionValidationRule(), expression, errorMessage, null); 
       System.Windows.Controls.Validation.MarkInvalid(expression, error); 

       if (_firstInvalidElement == null) 
       { 
        _firstInvalidElement = element; 
       } 
       return; 
      } 
     }); 
    } 
} 

和VB.Net相當於我得到的是:

For Each knownBinding As Binding In allKnownBindings 
    Dim errorMessage As String = DirectCast(Me.DataContext, IDataErrorInfo)(knownBinding.Path.Path) 
    If errorMessage IsNot Nothing AndAlso errorMessage.Length > 0 Then 
     isValid = False 

     ''# Display the error on any elements bound to the property 
     FindBindingsRecursively(Me.Parent, Function(element As FrameworkElement, binding As Binding, dp As DependencyProperty) Do 
      If knownBinding.Path.Path = Binding.Path.Path Then 

       Dim expression As BindingExpression = element.GetBindingExpression(dp) 
       Dim [error] As New ValidationError(New ExceptionValidationRule(), expression, errorMessage, Nothing) 
       System.Windows.Controls.Validation.MarkInvalid(expression, [error]) 

       If _firstInvalidElement Is Nothing Then 
        _firstInvalidElement = element 
       End If 

       Return 
      End If 
     End Function) 
    End If 
Next 
+2

什麼編譯錯誤,並在哪些行,你會得到? – ChrisF 2010-07-19 16:54:32

+0

沒有關於您收到的錯誤消息的更深層細節以及哪條線路導致錯誤,我們可能無法爲您提供幫助。 – 2010-07-19 16:58:55

+0

在If Knownbinding.path.path條件檢查後,我在上述代碼中發現錯誤。首先,它不能識別「Do」語句,並且有兩個「End Function」語句,所以在第一個結束語句之後的所有內容都是無效的 – Sai 2010-07-19 16:59:31

回答

0

您需要存儲在類中的可變參數才能使委託方法有權訪問它們。

Public Class BindingWork 
... 
Private binding As Binding 
Private path As String 
Private error_msg As String 

Public Sub Start() 

    Dim errinfo As IDataErrorInfo = CType(Me.DataContext, IDataErrorInfo) 
    For Each knownBinding As Binding In allKnownBindings 
     error_msg = errinfo(knownBinding.Path.Path) 
     If Not String.IsNullOrEmpty(error_msg) Then 
      isValid = False 
      Me.path = knownBinding.Path.Path 
      FindBindingsRecusively(Me.Parent, AddressOf WorkWithBindings) 
     End If 
    Next 

End Sub 

Sub WorkWithBindings(ByVal element As FrameworkElement, ByVal binding As Binding, ByVal dp As DependencyProperty) 
    If path = binding.Path.Path Then 
     Dim expression As BindingExpression = element.GetBindingExpression(dp) 
     Dim [error] As New ValidationError(New ExceptionValidationRule(), expression, error_msg, Nothing) 
     System.Windows.Controls.Validation.MarkInvalid(expression, [error]) 
     If _firstInvalidElement Is Nothing Then 
      _firstInvalidElement = element 
     End If 
    End If 
End Sub 
... 
End Class 

通知您的代理處理程序使用patherror_msg_firstInvalidElement和 '綁定'。

+0

感謝Jalexiou!我一定會嘗試這種方式並讓你知道。 – Sai 2010-07-20 07:58:31

3

試試這個free service但一定要爲它提供有效的C#代碼。


UPDATE:

我猜VB.NET編譯器窒息的原因是因爲傳遞給FindBindingsRecursively匿名函數的。嘗試外部化(deanonymize)此爲一個單獨的方法:

Sub FindASuitableName(element As FrameworkElement, binding As Binding, dp As DependencyProperty) 
    If knownBinding.Path.Path = binding.Path.Path Then 
     Dim expression As BindingExpression = element.GetBindingExpression(dp) 
     Dim [error] As New ValidationError(New ExceptionValidationRule(), expression, errorMessage, Nothing) 
     System.Windows.Controls.Validation.MarkInvalid(expression, [error]) 

     If _firstInvalidElement Is Nothing Then 
      _firstInvalidElement = element 
     End If 
    End If 
End Sub 

然後直接使用它:

FindBindingsRecursively(Me.Parent, FindASuitableName) 
+0

我已經使用過,和上面粘貼的一樣,vs2008給出了構建錯誤 – Sai 2010-07-19 16:55:14

+0

@Sai,請參閱我的更新。 – 2010-07-19 17:03:58

+0

謝謝Darin!但還有一個問題,我還需要在單獨的方法中使用knownBinding。其實方法FindBindingsRecursively有兩個參數1)元素作爲dependencyObject和2)callBackDelegate,所以我不能將knownbindings添加到單獨的方法。 – Sai 2010-07-19 17:12:12

0

考慮嘗試Telerik的C#/ VB轉換器。

http://converter.telerik.com/

它可以轉換滿檔,或摘要。

+0

我試過,但沒有改變。它與我上面給出的相同的代碼:( – Sai 2010-07-19 17:01:40