我有一个有两个后代班级的班级(孩子,孙子):
BaseSample
|-ProcessData
|-Measurement
当一个属性在Measurement类中发生变化时,我会引发一个OnCrucibleOxidizedMassChanged事件:
Protected Sub AddEventHandler(ByVal thisMeasurement As CalcinerDataAccess.O2Measurement)
AddHandler thisMeasurement.PropertyChanged, AddressOf RaiseMassChanged
End Sub
Protected Sub RaiseMassChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim CrucibleOxidizedMassReadyToReport As Boolean = _
(TypeOf e Is System.ComponentModel.PropertyChangedEventArgs) AndAlso _
(CType(e, System.ComponentModel.PropertyChangedEventArgs).PropertyName = "CrucibleOxidizedMass" And _
CType(sender, CalcinerDataAccess.O2Measurement).CrucibleOxidizedMass IsNot Nothing)
If CrucibleOxidizedMassReadyToReport Then
RaiseEvent OnCrucibleOxidizedMassChanged(sender, e)
End If
End Sub
出于测试目的,我有一个测试此事件提升行为的模块。请注意,ToteBinSample继承自BaseSample。在Main()中,我只是测试更改CrucibleOxidizedMass的值时会发生什么:
Sub Main()
thisSample = New SampleTypeManager.SampleTypes.ToteBinSample
AddHandler thisSample.OnCrucibleOxidizedMassChanged, AddressOf OnChanged
thisSample.SampleData.ProcessDatas(0).O2Measurements(0).CrucibleOxidizedMass = 20
Console.WriteLine("Done...")
Console.Read()
End Sub
Private Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("Time to send report!")
End Sub
然而,OnChanged子被调用两次,即使我只为CrucibleOxidizedMass赋值一次。
你知道为什么它被解雇了两次以及我如何纠正它?
答案 0 :(得分:0)
我发现了自己的错误。我在LINQ to SQL类的O2Measurement的OnCrucibleOxidizedMassChanged子项中手动引发了PropertyChanged事件。所以,我导致了PropertyChanged事件的第二次触发。谢谢,msarchet和其他任何思考过这个问题的人。
Partial Class O2Measurement
Private Sub OnCrucibleOxidizedMassChanged()
'Here is my mistake -- I have removed it in my code
RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("CrucibleOxidizedMass"))
End Sub
End Class