1

我想将以下 C# 代码转换为 VB.NET

/// <summary>
/// Attempts to raise the PropertyChanged event, and invokes the virtual AfterPropertyChanged
/// method, regardless of whether the event was raised or not.
/// </summary>
/// <param name="propertyName">
/// The property which was changed.
/// </param>

    protected void RaisePropertyChanged(string propertyName)
    {
        this.VerifyProperty(propertyName);

        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            //Get the cached event args.
            PropertyChangedEventArgs args = GetPropertyChangedEventArgs(propertyName);

            //Raise the PropertyChanged event.
            handler(this, args);
        }
    }

转换后的VB代码为:

''' <summary>
''' Attempts to raise the PropertyChanged event, and invokes the virtual AfterPropertyChanged
''' method, regardless of whether the event was raised or not.
''' </summary>
''' <param name="propertyName">
''' The property which was changed.
''' </param>
Protected Sub RaisePropertyChanged(propertyName As String)
    Me.VerifyProperty(propertyName)

    Dim handler As PropertyChangedEventHandler = Me.PropertyChanged

    If handler IsNot Nothing Then
        'Get the cached event args.
        Dim args As PropertyChangedEventArgs = GetPropertyChangedEventArgs(propertyName)

        'Raise the PropertyChanged event.
        handler.Invoke(Me, args)
    End If
End Sub

我使用了 Telerik 代码转换器,它没有提供关于事件和 XML 属性等的完美转换。

问题是,Visual Studio 显示以下错误:错误 103 'Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)' 是一个事件,不能直接调用。使用“RaiseEvent”语句来引发事件。

你能帮我解决这个问题吗,因为我不擅长 VB.NET 语法等:(

4

2 回答 2

3

只需将 Dim 处理程序 As PropertyChangedEventHandler = Me.PropertyChanged 替换为 Dim 处理程序 As PropertyChangedEventHandler = Me.PropertyChangedEvent

于 2015-01-12T05:04:00.243 回答
1

您需要先定义 PropertyChanged 变量!

像这样:公共事件 PropertyChangedEventHandler PropertyChanged;

于 2013-06-13T07:38:08.587 回答