locked
how to get the coordinates where there is a control on the screen? RRS feed

  • Question

  • User63474 posted

    how could I get the coordinates of x, and a control image??

    Monday, May 9, 2016 5:42 PM

Answers

All replies

  • User66163 posted

    @LuisGomez.7651

    X and Y are available as read only properties of the VisualElement base class of View.

    Please see for X: https://developer.xamarin.com/api/property/Xamarin.Forms.VisualElement.X/

    And for Y: https://developer.xamarin.com/api/property/Xamarin.Forms.VisualElement.Y/

    Hope this is what you were looking for,

    Tim

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, May 9, 2016 8:37 PM
  • User127538 posted

    Unfortunately this sometimes returns only relative coordination. To get more correct coordinates there is also a need to add control.Parent.Y and X values or even control.Parent.Parent.Y and X values. This is a real problem. I think this is because of StackLayout and Grid control layout mechanism but I am not sure.

    Tuesday, August 2, 2016 9:46 AM
  • User89714 posted

    @Luis_ingsis27 - You have to iterate up the UI hierarchy, adding the relative positions until you reach the top of the hierarchy.

    In (untested) pseudo-code:

    AbsoluteX = View.X
    AbsoluteY = View.Y
    while (View.Parent != null)
    {
        View = View.Parent;
        AbsoluteX += View.X
        AbsoluteY += View.Y
    }
    
    Tuesday, August 2, 2016 10:01 AM
  • User271938 posted

    Just to put a bow on this conversation, here's a method that returns the SCREEN coordinates of a view.

    ```csharp public (double X, double Y) GetScreenCoordinates(VisualElement view) { // A view's default X- and Y-coordinates are LOCAL with respect to the boundaries of its parent, // and NOT with respect to the screen. This method calculates the SCREEN coordinates of a view. // The coordinates returned refer to the top left corner of the view.

    // Initialize with the view's "local" coordinates with respect to its parent
    double screenCoordinateX = view.X;
    double screenCoordinateY = view.Y;
    
    // Get the view's parent (if it has one...)
    if (view.Parent.GetType() != typeof(App))
    {
        VisualElement parent = (VisualElement)view.Parent;
    
    
        // Loop through all parents
        while (parent != null)
        {
            // Add in the coordinates of the parent with respect to ITS parent
            screenCoordinateX += parent.X;
            screenCoordinateY += parent.Y;
    
            // If the parent of this parent isn't the app itself, get the parent's parent.
            if (parent.Parent.GetType() == typeof(App))
                parent = null;
            else
                parent = (VisualElement)parent.Parent;
        }
    }
    
    // Return the final coordinates...which are the global SCREEN coordinates of the view
    return (screenCoordinateX, screenCoordinateY);
    

    } ```

    Thursday, May 25, 2017 2:46 AM
  • User366108 posted

    Hi, I've been using this solution and so far it's great, unfortunately it fails if the control is inside a ListView cell (X an Y are always 0 for view cells). Anyone got a solution that works in these cases?

    Thanks

    Thursday, March 8, 2018 3:43 AM
  • User237759 posted

    In the different platforms, you can get the Renderer, which holds the Native View. From there, you can get the Screen coordinates. This does work in a Listview, I've used it myself. To get the Native Views:

    iOS:

    Xamarin.Forms.Platform.iOS.Platform.GetRenderer(myVisualElement).NativeView;
    

    iOS:

    Xamarin.Forms.Platform.iOS.Platform.GetRenderer(myVisualElement).View;
    

    UWP:

    Xamarin.Forms.Platform.iOS.Platform.GetRenderer(myVisualElement).FrameContainer;
    

    From there, you can get the screen coordinates on each platform. It may be helpful to get the native root view for some platforms.

    Tuesday, March 20, 2018 8:05 PM
  • User89714 posted

    @sonic1015 - You've listed iOS twice but not Android, and all three code snippets use Platform.iOS . Could you re-post as @Harshita has asked about this in another thread today ( https://forums.xamarin.com/discussion/124219/how-to-get-x-and-y-coordinate-of-a-litview-item )

    Wednesday, March 21, 2018 9:38 AM
  • User237759 posted

    Doh!

    That's what I get for posting in a hurry. Can't figure out how to edit though... here's the fixed syntax

    iOS:

    Xamarin.Forms.Platform.iOS.Platform.GetRenderer(myVisualElement).NativeView;
    

    Android:

    Xamarin.Forms.Platform.Android.Platform.GetRenderer(myVisualElement).View;
    

    UWP:

    Xamarin.Forms.Platform.UWP.Platform.GetRenderer(myVisualElement).FrameContainer;
    

    I'll post this in the other query as well as well, thanks for notifying me about the error.

    Wednesday, March 21, 2018 1:34 PM
  • User388125 posted

    I have a different question, I have screen coordinates obtained from the drag and drop of a label but I want to know the UI visual element where the label is dropped? Also the label coordinates after dragging relative to its parent control, for instance the X-coordinate sometimes is negative. I have posted a question on the forum

    https://forums.xamarin.com/discussion/169080/finding-ui-element-on-a-given-x-y-coordinates As I did not get a reply so thought to ask here.

    Tuesday, September 10, 2019 2:50 PM
  • User176749 posted

    @FactoryOptimizr said: Just to put a bow on this conversation, here's a method that returns the SCREEN coordinates of a view.

    ```csharp public (double X, double Y) GetScreenCoordinates(VisualElement view) { // A view's default X- and Y-coordinates are LOCAL with respect to the boundaries of its parent, // and NOT with respect to the screen. This method calculates the SCREEN coordinates of a view. // The coordinates returned refer to the top left corner of the view.

    // Initialize with the view's "local" coordinates with respect to its parent
    double screenCoordinateX = view.X;
    double screenCoordinateY = view.Y;
    
    // Get the view's parent (if it has one...)
    if (view.Parent.GetType() != typeof(App))
    {
        VisualElement parent = (VisualElement)view.Parent;
    
    
        // Loop through all parents
        while (parent != null)
        {
            // Add in the coordinates of the parent with respect to ITS parent
            screenCoordinateX += parent.X;
            screenCoordinateY += parent.Y;
    
            // If the parent of this parent isn't the app itself, get the parent's parent.
            if (parent.Parent.GetType() == typeof(App))
                parent = null;
            else
                parent = (VisualElement)parent.Parent;
        }
    }
    
    // Return the final coordinates...which are the global SCREEN coordinates of the view
    return (screenCoordinateX, screenCoordinateY);
    

    } ```

    it wont return the relative or absolute to screen. it returns only the positions to the parent view. So if you have a scrollview or stacklayout goes off the screen, it will return to the absolute x, y coordinates to the scrollview. To the screen is what is visible on the screen.

    Sunday, July 19, 2020 1:37 PM