为什么我的隧道事件参数对象和冒泡事件参数对象不相等?
我正在阅读 70-511 这本书,并且正在查看有关路由事件的部分。 我注意到它提到冒泡隧道事件对共享相同的 EventArgs 实例,因此如果您处理隧道事件(例如 PreviewMouseDown),它会停止配对的冒泡事件(例如 MouseDown);我已经尝试过这个并且它有效...但是,如果我每次事件处理程序触发时都测试相等性(出于测试目的,我对这两个事件使用 1 个事件处理程序),似乎 EventArgs 不是同一个实例(即它们具有不同的哈希值并且 Object.Equals 返回 false)... 如果我能弄清楚这是为什么,这将极大地提高我对路由事件如何工作的理解!
我们那里有任何 .NET 专家愿意解释一下吗?
我已经查看了 Pro WPF 书(优秀的书),这也只是说明: “为了让生活更有趣,如果将隧道事件标记为已处理,则冒泡事件将不会发生。这是因为这两个事件共享 RoutedEventArgs 类的相同实例。”
如果两个事件共享类的同一实例,则 eventargs 不应该具有相同的哈希值并为 Object.Equals 返回“True”吗???
private RoutedEventArgs args;
private void MouseDownHandler(object sender, MouseButtonEventArgs e)
{
listEvents.Items.Add(string.Format("{0} - {1} - {2} - {3}",
sender.GetType().Name, e.RoutedEvent.ToString(), e.Source.GetType().Name,
e.OriginalSource.GetType().Name));
listEvents.Items.Add(e.GetHashCode().ToString());
if (args != null) listEvents.Items.Add(e.Equals(args).ToString());
args = e;
}
XAML:
<Window x:Class="Chapter_2___WPF_RoutedEvents.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="428" Width="658"
PreviewMouseDown="MouseDownHandler" MouseDown="MouseDownHandler">
<Grid Name="grid"
MouseDown="MouseDownHandler" PreviewMouseDown="MouseDownHandler">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox Name="listEvents" Grid.Column="1"/>
<Button Content="Click Me!" Width="150" Height="50" Margin="10" Grid.Column="0"
MouseDown="MouseDownHandler" PreviewMouseDown="MouseDownHandler"/>
</Grid>
</Window>
I'm working through the 70-511 book, and am looking at the section on Routed Events.
I noticed it mentions that bubbling-tunneling event pairs share the same EventArgs instance, so if you handle the tunneling event (eg PreviewMouseDown), it halts the paired bubbling event (eg MouseDown); I've tried this and it works... But, if I test for equality each time the event handler fires (for test purposes I'm using 1 event handler for both events) it seems as though the EventArgs are NOT the same instance (ie they have different hashvalues and Object.Equals returns false)...
It would greatly improve my understanding of how routed events work if I could figure out why this is!
Any .NET gurus our there care to explain?
I've checked out the Pro WPF book (excellent book) and this also just states:
"To make life more interesting, if you mark the tunneling event as handled, the bubbling event won’t occur. That’s because the two events share the same instance of the RoutedEventArgs class."
If the two events share the SAME INSTANCE of a class, shouldn't the eventargs have the same hashvalues and return 'True' for Object.Equals???
private RoutedEventArgs args;
private void MouseDownHandler(object sender, MouseButtonEventArgs e)
{
listEvents.Items.Add(string.Format("{0} - {1} - {2} - {3}",
sender.GetType().Name, e.RoutedEvent.ToString(), e.Source.GetType().Name,
e.OriginalSource.GetType().Name));
listEvents.Items.Add(e.GetHashCode().ToString());
if (args != null) listEvents.Items.Add(e.Equals(args).ToString());
args = e;
}
The XAML:
<Window x:Class="Chapter_2___WPF_RoutedEvents.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="428" Width="658"
PreviewMouseDown="MouseDownHandler" MouseDown="MouseDownHandler">
<Grid Name="grid"
MouseDown="MouseDownHandler" PreviewMouseDown="MouseDownHandler">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox Name="listEvents" Grid.Column="1"/>
<Button Content="Click Me!" Width="150" Height="50" Margin="10" Grid.Column="0"
MouseDown="MouseDownHandler" PreviewMouseDown="MouseDownHandler"/>
</Grid>
</Window>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我运行您的代码并单击按钮时,它确实返回相同的哈希代码和
e.Equals(args)
的“True”。如果我再次单击,e.Equals(args)
返回“False”,因为每次单击都是RoulatedEventArgs
的新实例,但下一次返回True
因为隧道事件与冒泡事件相同。When I run your code and click the button, it does return the same hash code and 'True' for
e.Equals(args)
. If I click again,e.Equals(args)
returns 'False' because it is a new instance ofRoutedEventArgs
for each click, but the next one returnsTrue
because the tunneling event is the same as the bubbling event.