我只是在c#中玩弄矩形數組,一切都很好,直到我想循環幾次到應用程序(Silverlight)變得非常緩慢之後,在矩形的「圖層」之間循環。生成矩形後,應用程序變得緩慢
我的代碼如下,是否有可能提供任何建議,可能會導致這種退化?
private int worldHeight = 20;
private int currentLayerNo = 0;
private int keypressCounter = 0;
public MainPage()
{
InitializeComponent();
createGrid();
}
private void createGrid()
{
blockCanvas.Children.Clear();
int x = 0, y = 0;
Layer generateWorld = new Layer();
generateWorld.z = worldHeight;
generateWorld.x = 50;
generateWorld.y = 50;
generateWorld.Gen();
Rectangle[,] currentLayer = generateWorld.createLayer(currentLayerNo);
for (int a = 0; a < currentLayer.GetLength(0); a++)
{
for (int b = 0; b < currentLayer.GetLength(1); b++)
{
Rectangle currentBlock = new Rectangle();
currentBlock = currentLayer[a, b];
blockCanvas.Children.Add(currentBlock);
Canvas.SetTop(currentBlock, x);
Canvas.SetLeft(currentBlock, y);
y = y + 32;
}
x = x + 32;
y = 0;
}
currentLayer = null;
this.KeyDown += new KeyEventHandler(onKeyDown);
}
private void onKeyDown(object sender, KeyEventArgs e)
{
//Code here cycles between layers, each time calling the createGrid() method after altering currentLayerNo
}
}
也許會不時繪製一些圈子,讓您的應用再次受到動力? (嚴肅地說:如何將真正長的源代碼分解到真正有趣的部分?) –
我已經削減了一些代碼,對不起 - 關於這個 - 我想它有點多了:) – TimoneUK