How to get the child elements of a Absolute layout based on X,Y coordinates?

I am working to find the child element of an absolute layout based on the bounds of that child. There is an absolute layout which contains a label, I wan to find the child elements based on a set of coordinates. I can get the bounds of the label but when I try to use the bounds as a point in "GetChildElements" method, I am getting an error. It can be seen in the first attached screenshot that the I can get the bounds of a label but cannot use them a point. Can someone please check the code below and let me know what am I doing wrong? The error comes up when I run the last line of the code and use
GetChildElements() method.

PageElementPoint.xaml

<?xml version="1.0" encoding="utf-8" ?>![](https://us.v-cdn.net/5019960/uploads/editor/vd/ge0yb3dbuoiw.png "")

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"![](https://us.v-cdn.net/5019960/uploads/editor/55/iaexktapcdru.png "")

             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="TouchTrackingEffect.PageElementPoint">
    <ContentPage.Content>
        <!--Container Absolute Layout Padding="50" -->
        <AbsoluteLayout x:Name="absContainer">
            <!--Header  Label Style="{StaticResource headerStyle}" -->
            <Label x:Name="hdrLbl" Text="Arrange Page1 =>" AbsoluteLayout.LayoutBounds=".1,5,.4,80" AbsoluteLayout.LayoutFlags="XProportional,WidthProportional"></Label>
            <!-- Absolute Layout Structure Description in Middle  BackgroundColor="LightPink"       -->
            <AbsoluteLayout x:Name="absTopic" AbsoluteLayout.LayoutBounds=".1,.3,.5,.5" AbsoluteLayout.LayoutFlags="All">
                <Label x:Name="lblNew1" Text="First label" AbsoluteLayout.LayoutBounds="30, 30, 80, 50" BackgroundColor="LightSalmon"></Label>
                <!--<Label x:Name="lblNew2" Text="Second label"></Label>-->
            </AbsoluteLayout>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

PageElementPoint.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TouchTrackingEffect
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class PageElementPoint : ContentPage
    {
        public PageElementPoint()
        {
            InitializeComponent();
            // checking the child element at a point
            // finding the child elements of the absTopic at a Point
            Rectangle rectAbs = AbsoluteLayout.GetLayoutBounds(absTopic.Children[0]);
            Label lblNew = (Label)absTopic.Children[0];
            hdrLbl.Text = "Label Text " + lblNew.Text;
            hdrLbl.Text += "\n X= " + rectAbs.X + "Y= " + rectAbs.Y + " W= " + rectAbs.Width + " H=" + rectAbs.Height;
            Point p1 = new Point(rectAbs.X, rectAbs.Y);
            //hdrLbl.Text += "Child =" + absTopic.GetChildElements(p1).Count();
        }
    }
}


Answers

  • ElvisXiaElvisXia Member, Forum Administrator, Xamarin Team Xamurai

    @junejo_aisha AbsoluteLayout doesn't implement GetChildElements. If you search through the source codes you won't find any method named GetChildElements. The only reason why you can use it is because AbsoluteLayout inherits from Xamarin.Forms.View. But unfortunately, Xamarin.Forms.View.GetChildElement is hard coded, it returns null no matter which point you passed in.

    As a workaround you can use AbsoluteLayout.Children to query the child view that you want to target:

    example:

    var result = absTopic.Children.Where((e) => {
            var value = AbsoluteLayout.GetLayoutBounds(e).X;//if the X is the same.
            //same action for y
            return  value==rectAbs.X;
        });
        hdrLbl.Text += "Child =" + result.Count();
    
Sign In or Register to comment.