我在向网格添加控件时遇到性能问题。当我设置为添加 100 个控件时,在 Windows Phone 中需要将近 2 秒,并且它会冻结 UI。还有其他方法可以将自定义控件添加到网格吗?这是我的代码:
private void generateGrid(int size)
{
int buttonsInColumn = 5;
SmallButtonControl smallButton;
buttonEdge = screenWidth / buttonsInColumn;
for (int i = 0; i < buttonsInColumn; i++)
grChoolseBoard.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
for (int j = 0; j < size / buttonsInColumn; j++)
grChoolseBoard.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
for (int j = 0; j < size / buttonsInColumn; j++)
{
for (int i = 0; i < buttonsInColumn; i++)
{
smallButton = new SmallButtonControl() { Width = buttonEdge, Height = buttonEdge };
smallButton.Text = (j * buttonsInColumn + i + 1).ToString();
smallButton.ButtonBackground = getColorAndEnableState(j * buttonsInColumn + i + 1, _level+1,ref smallButton);
smallButton.SetValue(Grid.ColumnProperty, i);
smallButton.SetValue(Grid.RowProperty, j);
grChoolseBoard.Children.Add(smallButton as SmallButtonControl);
smallButton.Tapped += new TappedEventHandler(GoToBoard_Tapped);
}
}
}
在 C# 窗体中,为了避免在添加许多控件时出现延迟,SuspendLayout();
在添加控件时忽略更新 UI,然后ResumeLayout();
向 UI 发出再次开始显示更改的信号。 我不确定Windows Phone开发是否具有相同的功能。