c# - C# WPF OnMouseEnter 和 OnMouseLeave 循环
问题描述
我有 WPF UI 元素,当鼠标光标进入此元素时应隐藏,当鼠标光标离开此元素时可见。为此,我使用事件OnMouseEnter
& OnMouseLeave
,如下所示:
private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
(e.Source as UIElement).Visibility = Visibility.Hidden;
}
private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
{
(e.Source as UIElement).Visibility = Visibility.Visible;
}
<Canvas>
<Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="100" Canvas.Left="100" Fill="blue" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
<Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="150" Canvas.Left="150" Fill="yellow" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
<Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="200" Canvas.Left="200" Fill="green" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
<!-- Reverse the order to illustrate z-index property -->
<Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="300" Canvas.Left="200" Fill="green" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
<Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="350" Canvas.Left="150" Fill="yellow" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
<Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="400" Canvas.Left="100" Fill="blue" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
</Canvas>
但是当我运行程序并将光标悬停在元素上时,它开始闪烁并消耗大量 CPU 资源。
调试表明,当我将光标悬停在元素上时,事件会循环,直到我将光标移出元素。
我看到了这篇文章,但我不明白那里附加的解决方案。
我需要做些什么来防止这些事件循环?
解决方案
当您将 UIElement 的 Visibility 更改为 Hidden 时,您实际上触发了 MouseLeave 事件,因为现在在其后面的元素上执行了鼠标点击测试。然后运行您的事件处理程序,它将 Visibility 设置为 Visible,然后触发 MouseEnter 事件。因此闪烁。
解决这个问题的一个想法是使用不透明度而不是可见性。尝试:
private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
(e.Source as UIElement).Opacity = 0;
}
private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
{
(e.Source as UIElement).Opacity = 1;
}
推荐阅读
- android - 在 Google Play 管理中心上传 APK 失败
- nginx - nginx 实时自适应比特率流:- 无法手动切换质量?
- reactjs - 如何将添加键添加到反应选择多?
- python - 从 .py tensorflow 导入 dll 错误创建 .exe
- javascript - 在 JavaScript 中使用带有字母的 unix 时间戳时日期无效
- c# - C#:在 FlowLayoutPanel 中使用 backgroundworker 更改我的矩形的颜色
- java - 我无法访问 Eclipse 中任何现有类的方法。选择方法的自动建议功能不起作用
- postgresql - 我们如何获取 PostgreSQL 使用的主键的默认索引列表?
- r-exams - Exams2nops (r-exams) 的介绍页面
- java - Maven 工件 *-classes.jar 两次部署到存储库 - attachClasses