I am developing a mobile app to remotely control a WPF UI. The UI is managed by a third party and that third party can add\remove buttons any time. I am streaming the UI to the client Windows store app and it is sending the touch coordinates to server. Server then use HitTest to get the element on WPF UI and run the action.
It works most of the time, but the filter callback is not getting called some times. This is a dummy code.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Point p = new Point(77, 45);
// Coordinates received on server comes here
VisualTreeHelper.HitTest(this,
new HitTestFilterCallback(FilterCallback),
new HitTestResultCallback(ResultCallback),
new PointHitTestParameters(p));
}
private HitTestFilterBehavior FilterCallback( DependencyObject target)
{
System.Diagnostics.Debug.WriteLine(target.GetType());
if (typeof(Button) == target.GetType())
{
Button b = (Button)target;
Button_Click(b, null);
return HitTestFilterBehavior.Stop;
}
else
{
return HitTestFilterBehavior.Continue;
}
}
private HitTestResultBehavior ResultCallback(HitTestResult result)
{
System.Diagnostics.Debug.WriteLine(result.VisualHit.GetType());
return HitTestResultBehavior.Stop;
}
You have to return Continue
from your HitTestResultCallback. Otherwise hit testing won't traverse the complete visual tree, and may stop before it has reached the Button control.
private HitTestResultBehavior ResultCallback(HitTestResult result)
{
return HitTestResultBehavior.Continue;
}
See the Hit Testing in the Visual Layer article on MSDN for details.