PropertyChanged event fires twice and should not
I have a class with two descendent classes (child, grandchild):
BaseSample
|-ProcessData
|-Measurement
When a property changes in the M开发者_运维问答easurement class I raise a OnCrucibleOxidizedMassChanged event:
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
For testing purposes, I have a module that tests this event-raising behavior. Note that ToteBinSample inherits from the BaseSample. In Main() I'm just testing what happens when you change the value of 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
However, the OnChanged sub gets called twice, even though I only assign a value to CrucibleOxidizedMass once.
Do you have any idea why it gets fired twice and how I can correct it?
I found my mistake. I manually raised the PropertyChanged event within the OnCrucibleOxidizedMassChanged sub of the LINQ to SQL class, O2Measurement. So, I was causing the second firing of the PropertyChanged event. Thanks, msarchet, and any others who pondered this problem.
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
精彩评论