Xamarin Forms:单击重新启动按钮时网格按钮 UI 中断 [英] Xamarin Forms: Grid button UI breaks when click restart button

查看:20
本文介绍了Xamarin Forms:单击重新启动按钮时网格按钮 UI 中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 grid 中使用 button 来显示字母以实现 Word 搜索游戏.最初,UI 看起来不错,但是当单击 play again 按钮时,UI 会中断.

I am using a button inside the grid for showing letters to implement a Word search game. Initially, the UI is looking good, but when clicks the play again button the UI breaks.

截图:

网格内设置按钮的代码:

void SetGridLayout(char[,] matrixToPrint)
{
    int numRows = matrixToPrint.GetLength(0);
    int numCols = matrixToPrint.GetLength(1);

    gridLayout.HorizontalOptions = LayoutOptions.FillAndExpand;
    gridLayout.SetBinding(Button.HeightRequestProperty, new Binding("Width", source: gridLayout));

    for (int row = 0; row < numRows; row++)
    {
        gridLayout.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
    }
    for (int col = 0; col < numCols; col++)
    {
        gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
    }

    for (int rowIndex = 0; rowIndex < numRows; rowIndex++)
    {
        for (int columnIndex = 0; columnIndex < numCols; columnIndex++)
        {
            var Rowbutton = new Button
            {
                Text = Char.ToString(matrixToPrint[rowIndex, columnIndex]),
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Padding = 0,
                Margin = 0,
                BackgroundColor = Color.White
            };
            Rowbutton.SetBinding(Button.CommandProperty, "ClickCommand");
            Rowbutton.SetValue(Button.CommandParameterProperty, new PassObject(Rowbutton, rowIndex.ToString() + "," + columnIndex.ToString()));
            Rowbutton.SetBinding(Button.HeightRequestProperty, new Binding("Width", source: Rowbutton));
            gridLayout.Children.Add(Rowbutton, columnIndex, rowIndex);
        }
    }
}

我尝试了很多来找到这个问题背后的原因,但没有运气.我已在此处上传了一个示例项目以供参考.提前致谢.

I tried a lot to find the cause behind this issue, but no luck. I have uploaded a sample project here for the reference. Thanks in advance.

推荐答案

您正在向现有的 Grid 添加新的行和列,而没有先清除它

you are adding new rows and cols to an existing Grid without clearing it first

这篇关于Xamarin Forms:单击重新启动按钮时网格按钮 UI 中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆