VB.NET相当于C#代码

时间:2010-07-19 16:52:46

标签: wpf c#-to-vb.net

我在C#中有一个代码片段但无法转换为VB.NET。我尝试过在线转换器,但VS2008总是会出现编译错误。非常感谢任何帮助。

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

3 个答案:

答案 0 :(得分:3)

试试这个free service,但请确保为其提供有效的C#代码。


更新:

我猜VB.NET编译器窒息的原因是因为传递给FindBindingsRecursively的匿名函数。尝试将其外部化(取消匿名化)为一个单独的方法:

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)

答案 1 :(得分:0)

考虑尝试使用Telerik C#/ VB转换器。

http://converter.telerik.com/

它可以转换完整的文件或片段。

答案 2 :(得分: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和“绑定”。