【问题标题】:Hit Testing on Windows Phone 8Windows Phone 8 上的命中测试
【发布时间】: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


    【解决方案1】:

    很遗憾,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.
    }
    

    【讨论】:

    • 谢谢,您能否看一下我更新的示例,并根据提供的信息提出获取点击元素的解决方案。
    • 我不确定,但您似乎想将TouchLocation.Position 传递给FindElementsInHostCoordinates()... 这似乎是您在回答时所做的。
    【解决方案2】:

    您可能应该使用VisualTreeHelper.FindElementsInHostCoordinates,因为documentation for this method 状态(在备注中):

    FindElementsInHostCoordinates 与其他框架中的 HitTest 方法基本相似。

    【讨论】:

      【解决方案3】:

      这是我的问题的解决方案:

      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);
                      }
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-24
        相关资源
        最近更新 更多