RaisePropertyChanged -将代码从c#转换为VB.NET时出错

本文关键字:VB NET 出错 转换 代码 RaisePropertyChanged | 更新日期: 2023-09-27 18:18:04

我想把下面的一段c#代码转换成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 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显示以下错误:'Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)'是一个事件,不能直接调用。使用'RaiseEvent'语句引发一个事件。

你能帮我这个,因为我不擅长VB。. NET语法等:(

RaisePropertyChanged -将代码从c#转换为VB.NET时出错

直接替换Dim handler作为PropertyChangedEventHandler = Me。propertychange与Dim handler作为PropertyChangedEventHandler = Me。PropertyChangedEvent

您需要先定义PropertyChanged变量!

这样的: