C# 如何在所需位置显示模型窗口?

C# 如何在所需位置显示模型窗口?,c#,android,xamarin,xamarin.android,C#,Android,Xamarin,Xamarin.android,我有一个9*9的键盘模式窗口。并用它填充数独游戏板。 在安卓工作室, 为了获得按钮的位置,我使用了以下代码 _selectedButton=(Button)view; int tag=(int)_selectedButton.getTag(); _currentRow=tag/9; _currentCol=tag%9; int[] location=new int[2]; _selectedButton.getLocationOn

我有一个9*9的键盘模式窗口。并用它填充数独游戏板。 在安卓工作室, 为了获得按钮的位置,我使用了以下代码

_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();
键盘是这样的

int offsetX=30;
int offsetY=30;
_popupWindow.showAtLocation(_keyBoardLayout, Gravity.NO_GRAVITY, _p.x+offsetX, _p.y+offsetY);
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();
}
之后,他选择一个键,我关闭弹出窗口

public void BtnKey1Pressed(View view)
    {
        _selectedButton.setText("1");
        _popupWindow.dismiss();
    }
我如何在Xamarin Android中做到这一点。? 在xamarin中有可能得到这样的返回数据吗

int selectedKey=ShowKeyBoard();

我试图创建一个popupwindow的快速而肮脏的实现。我假设您想在刚才单击的按钮上显示弹出窗口,这就是我使用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();
    }
}

我试图创建一个popupwindow的快速而肮脏的实现。我假设您想在刚才单击的按钮上显示弹出窗口,这就是我使用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();
    }
}

请你具体说明你的问题好吗。要从ShowKeyboard获取返回值,只需使用返回值声明它并返回一个值;)我用android studio用Java编写的所有代码,我希望它是Xamarin android格式的。请指定您的问题。要从ShowKeyboard获取返回值,只需使用返回值声明它并返回一个值;)我用android studio用Java编写的所有代码,我希望它是Xamarin android格式。谢谢你的想法。虽然上面的代码不适用于我,但我根据上面的答案创建了类似的东西。我有一个问题,但,如果我点击最后一行按钮,然后弹出窗口将不会显示,因为它是出界。如果弹出窗口超出范围,我如何使其显示而不是显示在下方?因为您有弹出窗口的位置和实际高度,我建议检查位置+高度是否大于布局的高度。但是,您可能可以使用ShowAtLocation感谢您的想法。虽然上面的代码不适用于我,但我根据上面的答案创建了类似的东西。我有一个问题,但,如果我点击最后一行按钮,然后弹出窗口将不会显示,因为它是出界。如果弹出窗口超出范围,我如何使其显示而不是显示在下方?因为您有弹出窗口的位置和实际高度,我建议检查位置+高度是否大于布局的高度。但是,您可能可以使用ShowAtLocation