vb.net 中的变量/属性更改事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1996051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
variable/property changed event in vb.net
提问by Jonathan.
How can I make an event that is raised when a variable or property is changed (or can I just put the code I would put in an event in the Set section of a property?
如何创建在更改变量或属性时引发的事件(或者我可以将要放入事件的代码放在属性的 Set 部分?
回答by Aviad P.
From the MSDN library entry INotifyPropertyChanged.PropertyChanged Event:
从MSDN 库条目INotifyPropertyChanged.PropertyChanged Event:
Public Class DemoCustomer
Implements INotifyPropertyChanged
Private customerNameValue As String = String.Empty
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Public Property CustomerName() As String
Get
Return Me.customerNameValue
End Get
Set(ByVal value As String)
If Not (value = customerNameValue) Then
Me.customerNameValue = value
NotifyPropertyChanged("CustomerName")
End If
End Set
End Property
End Class
回答by David Rutten
Yes, the best (if not the only) way of doing this is to completely hide the actual variable (make it Private) and expose it via a Property which fires events whenever the setter is used.
是的,最好的(如果不是唯一的)方法是完全隐藏实际变量(使其成为私有)并通过属性公开它,该属性在使用 setter 时触发事件。
I do this regularly, but I've found that it's important to NOT raise the event if the new value is similar to the old value. This both eliminates unnecessary function calls and sometimes prevents recursive events.
我经常这样做,但我发现如果新值与旧值相似,则不要引发事件很重要。这既消除了不必要的函数调用,有时又防止了递归事件。
回答by itowlson
The canonical event for this is the PropertyChangedevent, which is defined in the INotifyPropertyChangedinterface. You can raise this from your property setters. (Note that VB.NET will not raise the event for you: you must include the code to raise it.)
对此的规范事件PropertyChanged是在INotifyPropertyChanged接口中定义的事件。你可以从你的财产设置者那里提出这个。(请注意,VB.NET 不会为您引发事件:您必须包含引发它的代码。)
If the code you want to run on a change is logically part of your class (e.g. updating a customer status when the balance changes) then it is appropriate to put this in the property setter rather than in an event handler. Event handlers are for when external code needs to know about changes, e.g. to update the UI in response to the customer status changing.
如果您要在更改时运行的代码在逻辑上是您的类的一部分(例如,当余额更改时更新客户状态),那么将其放在属性设置器中而不是事件处理程序中是合适的。事件处理程序用于外部代码需要了解更改的情况,例如更新 UI 以响应客户状态的更改。
回答by Rory
Take a look at this exampleimplementation of the INotifyPropertyChanged interface, this is the standard method used for this functionality. The important parts are the NotifyPropertyChanged method and the code inside the property set handlers.
看看这个INotifyPropertyChanged 接口的示例实现,这是用于此功能的标准方法。重要的部分是 NotifyPropertyChanged 方法和属性集处理程序中的代码。

