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

Luis_ingsis27Luis_ingsis27 MXMember ✭✭

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

Best Answer

Answers

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

  • JohnHardmanJohnHardman GBUniversity mod

    @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
    }
    
  • FactoryOptimizrFactoryOptimizr USUniversity ✭✭

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

    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);
    }
    
  • POFerroPOFerro Member

    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

  • sonic1015sonic1015 CAMember ✭✭

    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.

  • JohnHardmanJohnHardman GBUniversity mod

    @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 )

  • sonic1015sonic1015 CAMember ✭✭

    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.

  • junejo_aishajunejo_aisha Member ✭✭

    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.

Sign In or Register to comment.