Skip to main content
 首页 » 编程设计

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

2024年02月13日14me-sa

我想将以下 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 语法等等:(

请您参考如下方法:

简单替换 昏暗的处理程序作为 PropertyChangedEventHandler = Me.PropertyChanged 和 Dim 处理程序 As PropertyChangedEventHandler = Me.PropertyChangedEvent