开发者

VB.NET equivalent of C# code

I have a code snippet in C# but unable to convert to VB.NET. I tried online converters but VS2008 always gives compile errors. Any help is greatly appreciated.

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;
            }
        });
    }
}

and the VB.Net equivalent I got is:

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


Try this free service but make sure you provide it with valid C# code.


UPDATE:

I guess the reason the VB.NET compiler is choking is because of the anonymous function passed to FindBindingsRecursively. Try externalizing (deanonymize) this into a separate method:

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

And then use it directly:

FindBindingsRecursively(Me.Parent, FindASuitableName)


There are varariables that you need to store in a class in order for the delegate method to have access to them.

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

Notice your delegate handler uses path, error_msg, _firstInvalidElement and 'binding'.


Consider trying the Telerik C#/VB converter.

http://converter.telerik.com/

It can convert full files, or snippets.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜