【发布时间】:2016-10-06 09:36:23
【问题描述】:
我有一个带有上下文菜单的列表,当我单击时,选项列表(编辑、删除、信息)将显示在弹出窗口中。
这里我需要在上下文菜单图标的左下方显示这个弹出窗口,因为我需要上下文菜单相对于屏幕的位置。
谁能帮我在 Xamarin 中获取元素的 XY 位置?
提前致谢!
【问题讨论】:
标签: c# xamarin position xamarin.forms
我有一个带有上下文菜单的列表,当我单击时,选项列表(编辑、删除、信息)将显示在弹出窗口中。
这里我需要在上下文菜单图标的左下方显示这个弹出窗口,因为我需要上下文菜单相对于屏幕的位置。
谁能帮我在 Xamarin 中获取元素的 XY 位置?
提前致谢!
【问题讨论】:
标签: c# xamarin position xamarin.forms
这是我在Xamarin forums中找到的方法的版本:
public static class ScreenCoords
{
/// <summary>
/// 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.
/// </summary>
public static Point GetScreenCoords(this VisualElement view)
{
var result = new Point(view.X, view.Y);
while (view.Parent is VisualElement parent)
{
result = result.Offset(parent.X, parent.Y);
view = parent;
}
return result;
}
【讨论】:
NavigationPage,这将不包括导航栏的高度! (至少在Android上)似乎没有从 Xamarin Forms 获取导航栏高度的方法,因此获取实际屏幕坐标似乎需要a custom NavigationPageRenderer。哎呀。