简体   繁体   English

将代码形式vb.net转换为C#时出现问题

[英]Problem in converting the code form vb.net to c#

VB Code: VB代码:

Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
  If PropertyChangedEvent IsNot Nothing Then
    RaiseEvent PropertyChanged(Me, e)
  End If
End Sub

Converted C# code 转换后的C#代码

public event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged;

public delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e);

public void OnPropertyChanged(PropertyChangedEventArgs e)
{
    if (PropertyChangedEvent != null) {
        if (PropertyChanged != null) {
            PropertyChanged(this, e);
        }
    }
}

Error is: 错误是:

Error 1 The name 'PropertyChangedEvent' does not exist in the current context 错误1名称'PropertyChangedEvent'在当前上下文中不存在

Your event is called "PropertyChanged", not "PropertyChangedEvent". 您的事件称为“ PropertyChanged”,而不是“ PropertyChangedEvent”。
Also, the event is explicitly implemented, which means, you'd have to use this: ((INotifyPropertyChanged)this).PropertyChanged instead of PropertyChanged to access the event. 同样,该事件是显式实现的,这意味着,您必须使用以下命令: ((INotifyPropertyChanged)this).PropertyChanged而不是PropertyChanged才能访问该事件。
And as Oded pointed out, the code checks twice for the event. 正如Oded指出的那样,代码对事件进行了两次检查。 You can remove one of those checks. 您可以删除其中一张支票。

Don't use explicit interface implementation but just make it a public method. 不要使用显式接口实现,而应将其设为公共方法。

Or cast this to the interface to call the handler. 或投this的接口调用处理程序。 ((INotifyPropertyChanged)this).PropertyChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM