如何在所需位置显示模型 window?

How to display Model window at desired location?

我有一个键盘模式 window,它是 9*9。并用它来填充数独游戏板。 在 android 工作室, 为了获取按钮的位置,我使用了以下代码

_selectedButton=(Button)view;

        int tag=(int)_selectedButton.getTag();
        _currentRow=tag/9;
        _currentCol=tag%9;
int[] location=new int[2];
        _selectedButton.getLocationOnScreen(location);
        _p=new Point();
        _p.x=location[0];
        _p.y=location[1];

        ShowKeyBoard();

ShowKeyBoard是这样的

int offsetX=30;
int offsetY=30;
_popupWindow.showAtLocation(_keyBoardLayout, Gravity.NO_GRAVITY, _p.x+offsetX, _p.y+offsetY);

之后他选择了一个键,我关闭了弹出窗口 window。

public void BtnKey1Pressed(View view)
    {
        _selectedButton.setText("1");
        _popupWindow.dismiss();
    }

我如何在 Xamarin Android 中做到这一点? 是否有可能在 xamarin 中获得这样的 return 数据?

int selectedKey=ShowKeyBoard();

我试图创建一个快速而肮脏的弹出窗口实现。我假设您想在刚刚单击的按钮上显示弹出窗口,这就是我使用 ShowAsDropDown 的原因。我留下了 GetLocationOnScreen 代码,例如,您只需传递它即可。

public sealed class MyPopup : PopupWindow
{
    private readonly Action<int> _callbackMethod;

    private MyPopup(Activity context, Action<int> callbackMethod)
        : base(context.LayoutInflater.Inflate(Resource.Layout.Popup, null),
            ViewGroup.LayoutParams.WrapContent,
            ViewGroup.LayoutParams.WrapContent)
    {
        _callbackMethod = callbackMethod;
    }

    public static Task<int> GetNumber(Activity mainActivity, Button button)
    {
        var t = new TaskCompletionSource<int>();
        var popupWindow = new MyPopup(mainActivity, i => t.TrySetResult(i));
        popupWindow.Show(button);
        return t.Task;
    }

    private void Show(View anchor)
    {
        SetActionForChildButtons(anchor, View_Click);
        ShowAsDropDown(anchor);
    }

    private void SetActionForChildButtons(View parent, EventHandler e)
    {
        var button = parent as Button;
        if (button != null)
        {
            button.Click += e;
            return;
        }

        var viewGroup = parent as ViewGroup;
        if (viewGroup == null)
            return;

        for (var i = 0; i < viewGroup.ChildCount; i++)
        {
            var view = viewGroup.GetChildAt(i);
            SetActionForChildButtons(view, e);
        }
    }

    private void View_Click(object sender, EventArgs e)
    {
        var button = sender as Button;
        if (button == null)
            return;

        int number;
        if (int.TryParse(button.Text, out number))
            _callbackMethod?.Invoke(number);

        Dismiss();
    }
}

有了这个,您可以像这样以某种方式获得您的领域的编号

private async void OnClick(object sender, EventArgs e)
{
    var button = sender as Button;
    if (button == null)
        return;
    var location = new int[2];
    button.GetLocationOnScreen(location);
    var number = await MyPopup.GetNumber(this, button);
    button.Text = number.ToString();
}