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


Best Answer

Answers

  • junejo_aishajunejo_aisha Member ✭✭

    @ElvisXia Thanks for your reply. I also implemented it using Children of the absolute layout.

Sign In or Register to comment.