通知对象引用已更改 属性
Notify object references with some property changed
我需要通知对象引用 属性 已更改,检查以下代码:
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?
显而易见的解决方案是声明 Symbol
字段 WithEvents
。然后您可以将该字段包含在 Handles
子句中,就像您在表单上使用控件一样。您可以使用代码顶部的导航栏 window 来生成事件处理程序,就像在表单上使用控件一样:
Public Class Position
Private WithEvents _symbol As Symbol
Public Property Symbol As Symbol
Get
Return _symbol
End Get
Set
_symbol = value
End Set
End Property
Public Sub New(symbol As Symbol)
Me.Symbol = symbol
End Sub
Public Sub Symbol_PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Handles _symbol.PropertyChanged
Debug.Print($"New Price {Symbol.Price}")
End Sub
End Class
我冒昧地也正确地实施了 class 的其余部分。
我需要通知对象引用 属性 已更改,检查以下代码:
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?
显而易见的解决方案是声明 Symbol
字段 WithEvents
。然后您可以将该字段包含在 Handles
子句中,就像您在表单上使用控件一样。您可以使用代码顶部的导航栏 window 来生成事件处理程序,就像在表单上使用控件一样:
Public Class Position
Private WithEvents _symbol As Symbol
Public Property Symbol As Symbol
Get
Return _symbol
End Get
Set
_symbol = value
End Set
End Property
Public Sub New(symbol As Symbol)
Me.Symbol = symbol
End Sub
Public Sub Symbol_PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Handles _symbol.PropertyChanged
Debug.Print($"New Price {Symbol.Price}")
End Sub
End Class
我冒昧地也正确地实施了 class 的其余部分。