how could I get the coordinates of x, and a control image??
@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
Answers
@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
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:
Just to put a bow on this conversation, here's a method that returns the SCREEN coordinates of a view.
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
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:
iOS:
UWP:
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 )
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:
Android:
UWP:
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
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.
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.