开发者

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

开发者 https://www.devze.com 2023-01-27 04:51 出处:网络
VB Code: Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

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

0

精彩评论

暂无评论...
验证码 换一张
取 消