C# Windows Phone 8上的命中测试
我想在Windows Phone上执行命中测试。我在MSDN上找到了一篇关于它的文章,但我无法引用HitResult类或HitTest方法 并包含代码:C# Windows Phone 8上的命中测试,c#,xaml,windows-phone-8,hittest,C#,Xaml,Windows Phone 8,Hittest,我想在Windows Phone上执行命中测试。我在MSDN上找到了一篇关于它的文章,但我无法引用HitResult类或HitTest方法 并包含代码: // Respond to the left mouse button down event by initiating the hit test. private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Retrieve the c
// 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>
//
//
…您可能应该使用
VisualTreeHelper.FindElementsInHostCoordinates
,作为状态(在备注中):
FindElementsInHostCoordinates
与其他框架中的HitTest方法基本相似
不幸的是,
HitTestResult
等在Windows Phone上不可用(有一个文档错误,它错误地将所有.NET 4.5与Windows Phone关联)。不过,您可以使用VisualTreeHelper
获取所需内容。您可以处理Tap
事件,然后使用坐标来告知所点击的控件。例如:
private void OnTap(object sender, GestureEventArgs e)
{
var element = (UIElement)sender;
var controls = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(element), element);
// TODO: something with controls, like search for type Button, etc.
}
这就是我问题的解决方案:
private void timer_Tick(object sender, EventArgs e)
{
TouchCollection touchCollection = TouchPanel.GetState();
var element = ContentPanel as UIElement;
foreach (TouchLocation tl in touchCollection)
{
if ((tl.State == TouchLocationState.Pressed)
|| (tl.State == TouchLocationState.Moved))
{
var controls = VisualTreeHelper.FindElementsInHostCoordinates(new System.Windows.Point(tl.Position.X, tl.Position.Y), element);
}
}
}
谢谢,请看一下我更新的示例,并根据提供的信息建议获取点击元素的解决方案。我不确定,但您似乎希望传递
触摸位置。定位到FindElementsInHostCoordinates()
。。。这似乎就是你在回答时所做的。