c# - 事件参数——它们应该反射(reflect)事件发生时的状态快照还是实时数据?

标签 c# wpf events overriding messagebox

传递给 OnPreviewMouseLeftButtonDown 重写的 args 对象似乎描述了当前(实时)鼠标按钮状态,而不是事件发生时存在的状态快照。

这是正确的行为吗?事件参数不应该反射(reflect)事件发生时的事件数据(快照)并且不应该自动更新以反射(reflect)实时数据吗?

谢谢你,


代码示例

protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
   // before MessageBox display, e.ButtonState = Pressed
   MessageBox.Show("OnPreviewMouseLeftButtonDown");
      
   // now, e.ButtonState = Released 
   base.OnPreviewMouseLeftButtonDown(e);
}

最佳答案

我发现了为什么 e.ButtonState 的值在事件处理程序中发生变化——每次访问该属性时,都会调用底层 MouseDevice它返回当前(实时)按钮状态与事件发生时存在的状态。 (感谢 Microsoft 的 Bob Bao 提供 pointing this out。)

我已经在 http://bengribaudo.com/blog/2010/07/26/38/event-arguments-static-snapshots 上写了关于此的博客.

关于c# - 事件参数——它们应该反射(reflect)事件发生时的状态快照还是实时数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3214392/

相关文章:

c# - 按字母顺序将值插入 .txt 文件

c# - 在 ListView 控件中显示数据表

macos - 有没有办法在 Mac OS X 上触发手势事件?

c# - 当您不再有上下文引用时如何从上下文中分离实体

c# - .NET Core 和 .NET Framework 交互

wpf - 播放完 MediaElement 后,如何再次播放?

events - ServiceStack中的ServerEventsClient并发模型

c# - 有没有办法知道FileSystemwatcher还剩下多少缓冲区?

C#//SendKeys.SendWait 仅在进程窗口最小化时起作用

c# - 从 CefSharp 网络浏览器获取 HTML 源代码