How to get the element's position of its parent element on iOS or Mac?

jinglian cui 41 Reputation points
2024-11-14T14:57:28.32+00:00
    <ScrollView>
        <Grid WidthRequest="500" HeightRequest="500" x:Name="TheGrid" BackgroundColor="Blue">
            <Frame x:Name="TheView" HorizontalOptions="End" VerticalOptions="End" BackgroundColor="Yellow" WidthRequest="30" HeightRequest="30"></Frame>
        </Grid>
    </ScrollView>

How to get the position of "TheView" which is belong to "TheGrid"?

The X and Y property of "TheView" is 0 which is not my expect.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,714 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 45,486 Reputation points Microsoft Vendor
    2024-11-15T01:55:13.1433333+00:00

    Hello,

    This is related to the initialization process of the Maui control. If you read the X and Y values ​​directly in the page's construction method, they will be 0 because the control has not yet been assigned a position.

    After the control is placed on the page, you can read its X, Y. This event corresponds to the SizeChanged event of the control. Please refer to the following code.

    TheView.SizeChanged += (s, e) =>
    {
        Console.WriteLine(TheView.X.ToString());
    };
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.