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.


<?xml version="1.0" encoding="utf-8" ?>![]( "")

<ContentPage xmlns=""
             xmlns:x=""![]( "")

        <!--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>-->

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
    public partial class PageElementPoint : ContentPage
        public PageElementPoint()
            // 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();


  • 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:


    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.