vb.net对象属性值已更改事件

时间:2016-03-22 08:18:47

标签: c# .net vb.net events inotifypropertychanged

我正在寻找一种为对象类实现属性更改事件的方法。只要某个值发生变化,就需要重新计算物品质量。

我搜索了一些主题并且可能找到了一个解决方案,对我来说唯一的问题是c#对我来说是不可读的(现在)。

Link to topic c#

在做了一些挖掘之后,如果还找到了一个vb.net解决方案,在答案中他们会参考MSDN页面。但那里的样本已被删除。

Link to topic vb.net

我也发现了page

  

如何:实施财产变更通知

但是,如果我检查当前框架,它会读到:"此主题不再可用"

所以我猜想在更改框架期间这个事件发生了什么变化?或者,这仍然是vb.net解决方案中描述的主动方法吗?

添加类

的代码
Public Class BodyComponent

' Basic properties that are required for all of the bodycompenents
' regardless of the type.

<Browsable(True)> Public Property Type()
<Browsable(True)> Public Property Height() As Double
<Browsable(True)> Public Property Thickness() As Double
<Browsable(True)> Public Property Elevation() As Double
<Browsable(True)> Public Property Auto_Diameter() As Boolean
<Browsable(True)> Public Property Diameter() As Double
<Browsable(True)> Public Property Mass() As Double


' Type Enum that defines what kind of body component is created.
Public Enum BodyComponentType
    Cylinder = 0001
    Cone_reduction = 0002
End Enum

和派生类

Public Class Body_Cylinder

' Get the base properties
Inherits BodyComponent

' Set new properties that are only required for cylinders
Public Property Segments() As Integer
Public Property LW_Orientation() As Double

Public Shared Count As Integer = 0

Public Sub New()
    count = count + 1
End Sub

' Remove Cylinder from collection and change the count
Public Shared Sub delete_cylinder(ByVal oItem As Integer, ByRef oBinding As BindingSource)

    oBinding.RemoveAt(oItem)
    Count = Count - 1

End Sub

' Calculate mass
Private Sub Calc_mass()

    ' HACK: create material list where rho is defined for other
    ' material types
    Dim rho As Double
    rho = 8

    Dim oVolume As Double
    oVolume = Math.PI / 4 * ((Diameter + 2 * Thickness) ^ 2 - Diameter ^ 2) * Height

    Mass = oVolume * rho
End Sub

1 个答案:

答案 0 :(得分:3)

this MSDN article 取自 VisualBasic.Net 的示例。

' This class implements a simple customer type 
' that implements the IPropertyChange interface.

Public Class DemoCustomer
    Implements INotifyPropertyChanged

    ' These fields hold the values for the public properties.
    Private idValue As Guid = Guid.NewGuid()
    Private customerName As String = String.Empty
    Private companyNameValue As String = String.Empty
    Private phoneNumberValue 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


    ' The constructor is private to enforce the factory pattern.
    Private Sub New() 
        customerName = "no data"
        companyNameValue = "no data"
        phoneNumberValue = "no data"

    End Sub 'New


    ' This is the public factory method.
    Public Shared Function CreateNewCustomer() As DemoCustomer 
        Return New DemoCustomer()

    End Function

    ' This property represents an ID, suitable
    ' for use as a primary key in a database.
    Public ReadOnly Property ID() As Guid
        Get
            Return Me.idValue
        End Get
    End Property


    Public Property CompanyName() As String 
        Get
            Return Me.companyNameValue
        End Get 
        Set
            If value <> Me.companyNameValue Then
                Me.companyNameValue = value
                NotifyPropertyChanged("CompanyName")
            End If
        End Set
    End Property

    Public Property PhoneNumber() As String 
        Get
            Return Me.phoneNumberValue
        End Get 
        Set
            If value <> Me.phoneNumberValue Then
                Me.phoneNumberValue = value
                NotifyPropertyChanged("PhoneNumber")
            End If
        End Set
    End Property
End Class

您的学习资料来源: