User interface Xamarin表单:单击“重新启动”按钮时网格按钮UI会中断
我在User interface Xamarin表单:单击“重新启动”按钮时网格按钮UI会中断,user-interface,button,xamarin.forms,grid,User Interface,Button,Xamarin.forms,Grid,我在网格中使用按钮显示字母,以实现单词搜索游戏。最初,UI看起来不错,但当单击再次播放按钮时,UI会中断 屏幕截图: void SetGridLayout(char[,] matrixToPrint) { int numRows = matrixToPrint.GetLength(0); int numCols = matrixToPrint.GetLength(1); gridLayout.HorizontalOptions = LayoutOptions.FillA
网格中使用按钮
显示字母,以实现单词搜索游戏
。最初,UI看起来不错,但当单击再次播放
按钮时,UI会中断
屏幕截图:
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);
}
}
}
网格内设置按钮的代码:
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);
}
}
}
void SetGridLayout(char[,]matrixToPrint)
{
int numRows=matrixToPrint.GetLength(0);
int numCols=matrixToPrint.GetLength(1);
gridLayout.HorizontalOptions=LayoutOptions.FillAndExpand;
SetBinding(Button.HeightRequestProperty,新绑定(“宽度”,来源:gridLayout));
for(int行=0;行
我花了很多时间去寻找这个问题背后的原因,但是没有运气。我上传了一个示例项目供参考。提前感谢。您正在向现有的网格添加新行和列
而没有首先清除它乍一看,您似乎正在向现有网格添加新行和列,而没有首先清除它