【发布时间】:2016-07-09 00:54:30
【问题描述】:
我正在寻找一种方法来为对象类实现属性更改事件。每当某个值发生变化时,都需要重新计算物品的质量。
我搜索了一些主题并且可能找到了解决方案,对我来说唯一的问题是c# 对我来说是不可读的(目前)。
在进行了更多挖掘之后,如果还找到了 vb.net 解决方案,则在答案中他们参考了 MSDN 页面。但是那里的样本被删除了。
我也发现了这个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
【问题讨论】:
-
我认为OP的意思是如何实现INotifyPropertyChanged接口:msdn.microsoft.com/en-us/library/ms229614%28v=vs.100%29.aspx
-
您可以优化您想要的内容,因为它可能就像不使用自动实现的属性一样简单。
-
@Plutonix 你能澄清一下吗?
-
这就是我要你做的。提供与此相关的部分类。
标签: c# .net vb.net events inotifypropertychanged