首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Xamarin中获取元素相对于屏幕的位置

问在Xamarin中获取元素相对于屏幕的位置
EN

Stack Overflow用户
提问于 2016-10-06 17:36:24
回答 1查看 3.9K关注 0票数 3

我有一个带有上下文菜单的列表,当我单击时,选项列表(编辑、删除、信息)将显示在弹出窗口中。

在这里,我需要将这个弹出窗口显示在下面的上下文菜单图标的左侧,因此我需要上下文菜单相对于屏幕的位置。

有没有人能帮我在Xamarin中找到元素的XY位置?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2018-08-10 08:17:36

这是我在Xamarin forums中找到的一个方法的版本

代码语言:javascript
复制
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)
    {
        Point result = new Point(view.X, view.Y);

        var parent = view.Parent as VisualElement;
        while (parent != null)
        {
            result = result.Offset(parent.X, parent.Y);
            parent = parent.Parent as VisualElement;
        }
        return result;
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39892518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档