Reputation: 6772
I would like to perform Hit Testing on Windows Phone. I found an article about it on MSDN but I am unable to reference HitResult class nor HitTest method.
This is the example link from MSDN and contained code:
// 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.
}
}
Is there any easy and reliable pre-made way how to perform hit testing?
Update:
I need to work with Multi-touch that is why there is a timer approach with getting a collection of touch locations.
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.
}
}
}
Part of XAML code:
<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>
//
...
Upvotes: 1
Views: 472
Reputation: 6772
This is the solution to my problem:
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);
}
}
}
Upvotes: 0
Reputation: 35869
Unfortunately, HitTestResult
etc. are not available on Windows Phone (there is a documentation bug that incorrectly associates all of .NET 4.5 with Windows Phone). You can use VisualTreeHelper
to get at what you want though. You can handle a Tap
event then use the coordinates to tell what control was tapped. For example:
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.
}
Upvotes: 2
Reputation: 4198
You should probably use VisualTreeHelper.FindElementsInHostCoordinates
, as the documentation for this method states (in Remarks):
FindElementsInHostCoordinates
is fundamentally similar to the HitTest methods in other frameworks.
Upvotes: 1