how could I get the coordinates of x, and a control image??
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,
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.
@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
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;
parent = (VisualElement)parent.Parent;
// Return the final coordinates...which are the global SCREEN coordinates of the view
return (screenCoordinateX, screenCoordinateY);
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?
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:
From there, you can get the screen coordinates on each platform. It may be helpful to get the native root view for some platforms.
@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 )
That's what I get for posting in a hurry. Can't figure out how to edit though... here's the fixed syntax
I'll post this in the other query as well as well, thanks for notifying me about the error.
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
As I did not get a reply so thought to ask here.
Xamarin Inc., as a wholly-owned Microsoft subsidiary acting as a separate legal entity, adheres to the Microsoft Privacy Statement: Privacy & cookies