0
應該我有兩個派生類(子,孫子女)類:PropertyChanged事件觸發兩次,不
BaseSample
|-ProcessData
|-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一次。
你有什麼想法爲什麼它會被觸發兩次以及我如何糾正它?
你可以顯示你在哪裏舉辦初始活動。 – msarchet 2011-03-14 14:11:47
msarchet:在Sub Main()中,當我將20的值賦給CrucibleOxidizedMass屬性時,這應該觸發Measurement類中的PropertyChanged事件。在這個類中,我將PropertyChanged事件委託給RaiseMassChanged子類,它應該在正確的條件下提高OnCrucibleOxidizedMassChanged。正是這個事件導致模塊中的OnChanged子被調用(並且只打印一次「發送報告的時間!」)。希望這可以澄清我的問題。謝謝。 – blueshift 2011-03-14 15:01:41