Xamarin表单:单击重新启动按钮时,网格按钮UI中断 [英] Xamarin Forms: Grid button UI breaks when click restart button
本文介绍了Xamarin表单:单击重新启动按钮时,网格按钮UI中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 grid
内的 button
来显示字母,以实现 Word搜索游戏
.最初,UI看起来不错,但是当再次单击 play
按钮时,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表单:单击重新启动按钮时,网格按钮UI中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文