我有一个带有上下文菜单的列表,当我点击时,弹出窗口中会显示选项列表(编辑,删除,信息)。
在这里,我需要将下面的弹出窗口显示在上下文菜单图标中,因为我需要相对于屏幕的上下文菜单的位置。
任何人都可以帮我在Xamarin中获取元素的XY位置吗?
提前致谢!
答案 0 :(得分:1)
这是我在Xamarin forums中找到的方法的版本:
public static class ScreenCoords
{
public static (double X, double Y) GetScreenCoordinates(this 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.
var screenCoordinateX = view.X;
var screenCoordinateY = view.Y;
var parent = (VisualElement)view.Parent;
while (parent != null && parent.GetType().BaseType == typeof(View))
{
screenCoordinateX += parent.X;
screenCoordinateY += parent.Y;
parent = (VisualElement)parent.Parent;
}
return (screenCoordinateX, screenCoordinateY);
}
}