【发布时间】:2020-09-18 16:01:20
【问题描述】:
我需要通知对象引用的属性发生变化,检查以下代码:
Public Class Symbol
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(<Runtime.CompilerServices.CallerMemberName> Optional ByVal propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private _Price As Decimal
Public Property Price() As Decimal
Get
Return _Price
End Get
Set(ByVal value As Decimal)
If Not (value = _Price) Then
_Price = value
NotifyPropertyChanged()
End If
End Set
End Property
End Class
Public Class Position
Public Symbol As Symbol
Public Sub New(symbol As Symbol)
Me.Symbol = symbol
End Sub
Public Sub PriceChanged()
Debug.Print($"New Price {Symbol.Price}")
End Sub
End Class
当 Symbol 价格发生变化时,如何让 PriceChanged 开始?
【问题讨论】:
-
你看过AddHandler吗?
-
是的,伙计,但如果可能的话,首先需要使用propertyChanged 实现。
-
这就是
INotifyPropertyChanged模式的工作原理。您在 NotifyPropertychanged 生产者类中引发事件,并拥有侦听通知的消费者类。为此,您宣布了一个公共事件 (Public Event PropertyChanged)。 -
你也可以使用 Bindings 和 BindingSource。
标签: .net vb.net oop interface inotifypropertychanged