【问题标题】:VisualTreeHelper.FindElementsInHostCoordinates with Rectangle in Canvas ? Never find my UIElementVisualTreeHelper.FindElementsInHostCoordinates 与 Canvas 中的矩形?永远找不到我的 UIElement
【发布时间】:2012-06-22 15:56:25
【问题描述】:

我正在尝试从我的 Silverlight for WP7 应用程序中的坐标中找到 UIElement

这是我的 XAML:

<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<Canvas x:Name="ContentPanel" Grid.Row="1">
    <Rectangle Canvas.Top="20" Canvas.Left="20" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="90" Canvas.Left="20" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="160" Canvas.Left="20" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />

    <Rectangle Canvas.Top="20" Canvas.Left="90" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="90" Canvas.Left="90" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="160" Canvas.Left="90" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />

    <Rectangle Canvas.Top="20" Canvas.Left="160" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="90" Canvas.Left="160" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="160" Canvas.Left="160" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />

    <Rectangle Canvas.Top="20" Canvas.Left="230" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="90" Canvas.Left="230" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="160" Canvas.Left="230" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
</Canvas>

还有我的代码隐藏:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void Rectangle_Tap(object sender, GestureEventArgs e)
    {
        Rectangle r = (Rectangle)sender;

        double x = (double)r.GetValue(Canvas.LeftProperty);
        double y = (double)r.GetValue(Canvas.TopProperty);

        Debug.WriteLine(x + "," + y);

        var query = VisualTreeHelper.FindElementsInHostCoordinates(new Point(x - 70, y), ContentPanel).Union(
            VisualTreeHelper.FindElementsInHostCoordinates(new Point(x + 70, y), ContentPanel)).Union(
            VisualTreeHelper.FindElementsInHostCoordinates(new Point(x, y - 70), ContentPanel)).Union(
            VisualTreeHelper.FindElementsInHostCoordinates(new Point(x, y + 70), ContentPanel)).Union(
            VisualTreeHelper.FindElementsInHostCoordinates(new Point(x, y), ContentPanel));

        foreach (UIElement element in query.OfType<Rectangle>())
        {
            // never goes here
            ContentPanel.Children.Remove(element);
        }
    }
}

但问题是该方法永远不会返回我的矩形。

我的代码有什么问题?

提前感谢您的帮助。 最好的问候

【问题讨论】:

  • 您能具体描述一下您想要达到的目标吗?
  • 我的目的是检测触摸的uielement。这只是一个易于复制的简单示例

标签: silverlight windows-phone-7 uielement


【解决方案1】:

我不确定这是否返回正确的值(对您而言,因为您的问题不清楚),但您应该尝试以下方法

private void Rectangle_Tap(object sender, GestureEventArgs e)
{
    Point position = e.GetPosition(Application.Current.RootVisual);

    var query = VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X - 70, position.Y), Application.Current.RootVisual).Union(
        VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X + 70, position.Y), Application.Current.RootVisual)).Union(
        VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X, position.Y - 70), Application.Current.RootVisual)).Union(
        VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X, position.Y + 70), Application.Current.RootVisual)).Union(
        VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X, position.Y), Application.Current.RootVisual));

    foreach (UIElement element in query.OfType<Rectangle>())
    {
        // never goes here
        ContentPanel.Children.Remove(element);
    }
}

我使用的来源:

VisualTreeHelper.FindElementsInHostCoordinates Method (Point, UIElement) GestureEventArgs.GetPosition Method

【讨论】:

  • @anatoliiG 很高兴听到该解决方案有效。感谢测试
  • 很好,这很有效。只需要将 Application.RootVisual 替换为 Application.Current.RootVisual。我认为我没有很好的参考。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多