简体   繁体   中英

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

VB Code:

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

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

Your event is called "PropertyChanged", not "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.
And as Oded pointed out, the code checks twice for the event. 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. ((INotifyPropertyChanged)this).PropertyChanged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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