Event Arguments - Should they reflect state snapshots of the moment the event occured or live data?
It appears that the args object passed into an override of OnPreviewMous开发者_运维知识库eLeftButtonDown describes the current (live) mouse button state, not a snapshot of the state present when the event occurred.
Is this proper behavior? Shouldn't event arguments reflect event data at the moment the event occurred (a snapshot) and not be automatically updated to reflect live data?
Thank you,
BenCode Sample
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
// before MessageBox display, e.ButtonState = Pressed
MessageBox.Show("OnPreviewMouseLeftButtonDown");
// now, e.ButtonState = Released
base.OnPreviewMouseLeftButtonDown(e);
}
I found out why e.ButtonState's value changes in the event handler--each time that property is accessed, a call is made to an underlying MouseDevice
which returns the current (live) button state vs. the state that existed the moment the event occurred. (Thanks to Microsoft's Bob Bao for pointing this out.)
I've blogged about this at http://bengribaudo.com/blog/2010/07/26/38/event-arguments-static-snapshots.
Any instance members of EventArgs
class are not guaranteed to be thread safe. So I guess we can not complain if the internal state of the argument changes ;)
精彩评论