【发布时间】:2014-04-27 04:59:11
【问题描述】:
我想在 Windows Phone 上执行命中测试。我在 MSDN 上找到了一篇关于它的文章,但我无法引用 HitResult 类或 HitTest 方法。
This is the example link from MSDN 并包含代码:
// Respond to the left mouse button down event by initiating the hit test.
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Retrieve the coordinate of the mouse position.
Point pt = e.GetPosition((UIElement)sender);
// Perform the hit test against a given portion of the visual object tree.
HitTestResult result = VisualTreeHelper.HitTest(myCanvas, pt);
if (result != null)
{
// Perform action on hit visual object.
}
}
有没有简单可靠的预制方式来进行命中测试?
更新:
我需要使用多点触控,这就是为什么有一种计时器方法来获取触控位置的集合。
private void timer_Tick(object sender, EventArgs e)
{
TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
{
if ((tl.State == TouchLocationState.Pressed)
|| (tl.State == TouchLocationState.Moved))
{
// Get tapped element on the screen based on touch location.
// I am looking for the tapped rectangles that you can see in the XAML code.
}
}
}
部分 XAML 代码:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,30,12,0">
<Grid.Background>
<ImageBrush Stretch="Fill"/>
</Grid.Background>
<Rectangle x:Name="tile11" HorizontalAlignment="Left" Height="103" Margin="10,22,0,0" VerticalAlignment="Top" Width="103" >
<Rectangle.Fill>
<ImageBrush Stretch="Fill" ImageSource="/Assets/Tiles/DEFAULT.png"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="tile12" HorizontalAlignment="Left" Height="103" Margin="120,22,0,0" VerticalAlignment="Top" Width="103" >
<Rectangle.Fill>
<ImageBrush Stretch="Fill" ImageSource="/Assets/Tiles/DEFAULT.png"/>
</Rectangle.Fill>
</Rectangle>
//
...
【问题讨论】:
标签: c# xaml windows-phone-8 hittest