C#转VB转换问题

时间:2010-08-13 12:39:23

标签: c# vb.net

任何人都可以帮助我将此转换为vb.net我使用的转换器无法正常工作且真的不知道如何转换它。

public Person CurrentPersonCancellable
    {
        get
        {
            Debug.WriteLine("Getting CurrentPersonCancellable.");
            return _CurrentPersonCancellable;
        }
        set
        {
            // Store the current value so that we can 
            // change it back if needed.
            var origValue = _CurrentPersonCancellable;

            // If the value hasn't changed, don't do anything.
            if (value == _CurrentPersonCancellable)
                return;

            // Note that we actually change the value for now.
            // This is necessary because WPF seems to query the 
            //  value after the change. The combo box
            // likes to know that the value did change.
            _CurrentPersonCancellable = value;

            if (
                MessageBox.Show(
                    "Allow change of selected item?", 
                    "Continue", 
                    MessageBoxButton.YesNo
                ) != MessageBoxResult.Yes
            )
            {
                Debug.WriteLine("Selection Cancelled.");

                // change the value back, but do so after the 
                // UI has finished it's current context operation.
                Application.Current.Dispatcher.BeginInvoke(
                        new Action(() =>
                        {
                            Debug.WriteLine(
                                "Dispatcher BeginInvoke " + 
                                "Setting CurrentPersonCancellable."
                            );

                            // Do this against the underlying value so 
                            //  that we don't invoke the cancellation question again.
                            _CurrentPersonCancellable = origValue;
                            OnPropertyChanged("CurrentPersonCancellable");
                        }),
                        DispatcherPriority.ContextIdle,
                        null
                    );

                // Exit early. 
                return;
            }

            // Normal path. Selection applied. 
            // Raise PropertyChanged on the field.
            Debug.WriteLine("Selection applied.");
            OnPropertyChanged("CurrentPersonCancellable");
        }
    }

转换器给了我这个,我遇到问题的地方就是调用application.Current.Dispather.BeginInvoke。

Public Property CurrentPersonCancellable() As Person
Get
    Debug.WriteLine("Getting CurrentPersonCancellable.")
    Return _CurrentPersonCancellable
End Get
Set
    ' Store the current value so that we can 
    ' change it back if needed.
    Dim origValue = _CurrentPersonCancellable

    ' If the value hasn't changed, don't do anything.
    If value = _CurrentPersonCancellable Then
        Return
    End If

    ' Note that we actually change the value for now.
    ' This is necessary because WPF seems to query the 
    '  value after the change. The combo box
    ' likes to know that the value did change.
    _CurrentPersonCancellable = value

    If MessageBox.Show("Allow change of selected item?", "Continue", MessageBoxButton.YesNo) <> MessageBoxResult.Yes Then
        Debug.WriteLine("Selection Cancelled.")

        ' change the value back, but do so after the 
        ' UI has finished it's current context operation.
        Application.Current.Dispatcher.BeginInvoke(New Action(Function() Do
            Debug.WriteLine("Dispatcher BeginInvoke " + "Setting CurrentPersonCancellable.")

            ' Do this against the underlying value so 
            '  that we don't invoke the cancellation question again.
            _CurrentPersonCancellable = origValue
            OnPropertyChanged("CurrentPersonCancellable")
        End Function), DispatcherPriority.ContextIdle, Nothing)

        ' Exit early. 
        Return
    End If

    ' Normal path. Selection applied. 
    ' Raise PropertyChanged on the field.
    Debug.WriteLine("Selection applied.")
    OnPropertyChanged("CurrentPersonCancellable")
End Set
End Property

2 个答案:

答案 0 :(得分:1)

Function中使用的

BeginInvoke不能包含多个语句 您需要将其移动到单独的函数中并根据需要调用/获取它的地址。

有许多事情(特别是与lamdas和匿名方法有关)可以在C#中做,而在VB.Net中却无法做到。

有很多语言元素可以互换。

答案 1 :(得分:0)

试试这个......

Public Property CurrentPersonCancellable() As Person
    Get
        Debug.WriteLine("Getting CurrentPersonCancellable.")
        Return _CurrentPersonCancellable
    End Get
    Set
        ' Store the current value so that we can '
        ' change it back if needed.'
        Dim origValue = _CurrentPersonCancellable

        ' If the value hasnt changed, dont do anything.'
        If value = _CurrentPersonCancellable Then
            Return
        End If

        ' Note that we actually change the value for now.'
        ' This is necessary because WPF seems to query the '
        '  value after the change. The combo box'
        ' likes to know that the value did change.'
        _CurrentPersonCancellable = value

        If MessageBox.Show("Allow change of selected item?", "Continue", MessageBoxButton.YesNo) <> MessageBoxResult.Yes Then
            Debug.WriteLine("Selection Cancelled.")

            ' change the value back, but do so after the '
            ' UI has finished its current context operation.'
            Application.Current.Dispatcher.BeginInvoke(New Action(of Person)(addressof dispatcherCallerHelper), _
                                                       DispatcherPriority.ContextIdle, _
                                                       new Object() {origValue})

            ' Exit early. '
            Return
        End If

        ' Normal path. Selection applied. '
        ' Raise PropertyChanged on the field.'
        Debug.WriteLine("Selection applied.")
        OnPropertyChanged("CurrentPersonCancellable")
    End Set
End Property

private sub dispatcherCallerHelper(origValue as Person)
    Debug.WriteLine("Dispatcher BeginInvoke " & "Setting CurrentPersonCancellable.")

    ' Do this against the underlying value so '
    '  that we dont invoke the cancellation question again.'
    _CurrentPersonCancellable = origValue
    OnPropertyChanged("CurrentPersonCancellable")
end sub

另外,对于OnpropertyChanged函数,您应该考虑使用静态反射:

http://www.codeproject.com/Articles/36262/Getting-Fun-with-NET-Static-Reflection.aspx

干杯!