c# - MouseEnter 事件的奇怪行为

标签 c# xaml windows-phone-8 windows-phone

我正在开发 Windows Phone 8 XAML 应用程序。

我为 XAML 页面中的多个对象定义了 MouseEnter 事件。 (例如两个矩形)。触发这些对象的 MouseEnter 事件后,我按下同一页面上的一个按钮。按钮按下不仅会触发其自身的点击事件。它还会触发最后输入的 Rectangle 的 MouseEnter 事件。

这个问题可以通过一个只有两个矩形和一个按钮的新项目重现。这种行为并不总是出现,但可以在几次触发 Rectangles 的 MouseEnter 事件后观察到。

我的 XAML 对象如下。

<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100"
           Margin="102,306,0,0" Stroke="Black" 
           VerticalAlignment="Top" Width="100" 
           MouseEnter="Rectangle_MouseEnter"/>

<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" 
           Margin="314,306,0,0" Stroke="Black" 
           VerticalAlignment="Top" Width="100"
           MouseEnter="Rectangle_MouseEnter"/>

<Button Content="Button" HorizontalAlignment="Left"
        Margin="256,481,0,0" VerticalAlignment="Top" Click="Button_Click"/>

后面的代码看起来像这样。

private void Rectangle_MouseEnter(object sender, 
                                  System.Windows.Input.MouseEventArgs e)
{
    Debug.WriteLine("Rectangle_MouseEnter");
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("Button_Click");
}

最佳答案

如果我记得,在 Windows pone 和 Windows 8 应用程序中,最好使用点击事件而不是鼠标事件。这可能是问题所在。查看此链接:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.tapped.aspx

关于c# - MouseEnter 事件的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18836189/

相关文章:

c++ - 在 C++ 或 C++/CX 中检测 Windows Phone 版本

c# - 如何使用2*Xeon E5的电源?

c# - "The .NET framework uses the UTF-16 encoding standard by default"是什么意思?

windows-phone-8 - Windows Phone 8 支持捕获短信或来电?

xaml - 在 xaml 中使用自定义字体

c# - 动态地将 BoxView 添加到网格 [Xamarin.Forms]

xna - Windows Phone 8 SDK 博客公告和 XNA 混淆

c# - 为什么这个 LINQ 抛出 "FormatException was unhandled"?

c# - ASP 中的 Cache.NoSlidingExpiration - 何时重置?

c# - 在 WPF (MVVM) 中动态更改窗口的用户控件