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

时间:2013-06-13 05:24:04

标签: c# .net wpf vb.net-2010 inotifypropertychanged

我想将以下的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 Code Converter,它没有提供有关事件和XML属性等的完美转换。

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

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

2 个答案:

答案 0 :(得分:3)

简单地更换 Dim handler As PropertyChangedEventHandler = Me.PropertyChanged 同 Dim handler As PropertyChangedEventHandler = Me.PropertyChangedEvent

答案 1 :(得分:1)

您需要先定义PropertyChanged变量!

像这样:      公共事件PropertyChangedEventHandler PropertyChanged;