C# For-Loop 不将项目添加到 Observable 集合
C# For-Loop not adding Items to Observable Collection
所以我在使用 for 循环将值添加到可观察集合中时遇到了问题。我正在尝试向集合中添加 625 个值,但它跳过了一些值,我不知道为什么或者我看不到问题...
如代码中所示,我有两个集合,一个我已经在构造函数中预填充了值(没有问题),另一个我正尝试用新值填充。我有一个循环 625 次的 for 循环(同样有效)但不知何故跳过了一些值,我不知道为什么。
也许还可以说我有一个方法可以在单击 CellCollection 中的某些值时更改,但这发生在 for 循环之前并且实际上与它无关但也许值得一提。
使用 For 循环的方法:
void neighborCalc()
{
//calculates alive neighbors and kills or sets cell alive
for (int i = 0; i < CellCollection.Count; i++)
{
int neighborAliveCounter = 0;
//check top
if (CellCollection[i].Row > 1)
{
CellModel topCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column && cell.Row == CellCollection[i].Row - 1);
if (topCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check bottom
if (CellCollection[i].Row < 25)
{
CellModel bottomCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column && cell.Row == CellCollection[i].Row + 1);
if (bottomCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check left
if (CellCollection[i].Column > 1)
{
CellModel leftCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column - 1 && cell.Row == CellCollection[i].Row);
if (leftCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check right
if (CellCollection[i].Column < 25)
{
CellModel rightCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column + 1 && cell.Row == CellCollection[i].Row);
if (rightCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check topleft
if (CellCollection[i].Column > 1 && CellCollection[i].Row > 1)
{
CellModel topLeftCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column - 1 && cell.Row == CellCollection[i].Row - 1);
if (topLeftCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check top right cell
if (CellCollection[i].Row > 1 && CellCollection[i].Column < 25)
{
CellModel topRightCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column + 1 && cell.Row == CellCollection[i].Row - 1);
if (topRightCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check bottom left cell
if (CellCollection[i].Row < 25 && CellCollection[i].Column > 1)
{
CellModel bottomLeftCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column - 1 && cell.Row == CellCollection[i].Row + 1);
if (bottomLeftCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check bottom right cell
if (CellCollection[i].Row < 25 && CellCollection[i].Column < 25)
{
CellModel bottomRightCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column + 1 && cell.Row == CellCollection[i].Row + 1);
if (bottomRightCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//decides if to put cell alive or dead
if (neighborAliveCounter < 2 || neighborAliveCounter > 3)
{
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.Transparent), IsAlive = false });
}
if (neighborAliveCounter == 3)
{
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.LightSeaGreen), IsAlive = true });
}
}
}
我不确定为什么我在它们中没有相同数量的值,并且某些行/列不在缩进位置(这是合乎逻辑的,因为 tempCollection 不知何故没有 625 但更少的值。)
您这里没有处理 neighborAliveCounter == 2
的情况。这就是您得到错误计数的原因。下面的代码块只检查小于 2、等于 3 和大于 3。
//decides if to put cell alive or dead
if (neighborAliveCounter < 2 || neighborAliveCounter > 3)
{
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.Transparent), IsAlive = false });
}
if (neighborAliveCounter == 3)
{
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.LightSeaGreen), IsAlive = true });
}
所以我在使用 for 循环将值添加到可观察集合中时遇到了问题。我正在尝试向集合中添加 625 个值,但它跳过了一些值,我不知道为什么或者我看不到问题...
如代码中所示,我有两个集合,一个我已经在构造函数中预填充了值(没有问题),另一个我正尝试用新值填充。我有一个循环 625 次的 for 循环(同样有效)但不知何故跳过了一些值,我不知道为什么。
也许还可以说我有一个方法可以在单击 CellCollection 中的某些值时更改,但这发生在 for 循环之前并且实际上与它无关但也许值得一提。
使用 For 循环的方法:
void neighborCalc()
{
//calculates alive neighbors and kills or sets cell alive
for (int i = 0; i < CellCollection.Count; i++)
{
int neighborAliveCounter = 0;
//check top
if (CellCollection[i].Row > 1)
{
CellModel topCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column && cell.Row == CellCollection[i].Row - 1);
if (topCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check bottom
if (CellCollection[i].Row < 25)
{
CellModel bottomCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column && cell.Row == CellCollection[i].Row + 1);
if (bottomCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check left
if (CellCollection[i].Column > 1)
{
CellModel leftCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column - 1 && cell.Row == CellCollection[i].Row);
if (leftCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check right
if (CellCollection[i].Column < 25)
{
CellModel rightCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column + 1 && cell.Row == CellCollection[i].Row);
if (rightCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check topleft
if (CellCollection[i].Column > 1 && CellCollection[i].Row > 1)
{
CellModel topLeftCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column - 1 && cell.Row == CellCollection[i].Row - 1);
if (topLeftCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check top right cell
if (CellCollection[i].Row > 1 && CellCollection[i].Column < 25)
{
CellModel topRightCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column + 1 && cell.Row == CellCollection[i].Row - 1);
if (topRightCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check bottom left cell
if (CellCollection[i].Row < 25 && CellCollection[i].Column > 1)
{
CellModel bottomLeftCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column - 1 && cell.Row == CellCollection[i].Row + 1);
if (bottomLeftCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//check bottom right cell
if (CellCollection[i].Row < 25 && CellCollection[i].Column < 25)
{
CellModel bottomRightCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column + 1 && cell.Row == CellCollection[i].Row + 1);
if (bottomRightCell.IsAlive == true)
{
neighborAliveCounter = neighborAliveCounter + 1;
}
}
//decides if to put cell alive or dead
if (neighborAliveCounter < 2 || neighborAliveCounter > 3)
{
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.Transparent), IsAlive = false });
}
if (neighborAliveCounter == 3)
{
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.LightSeaGreen), IsAlive = true });
}
}
}
我不确定为什么我在它们中没有相同数量的值,并且某些行/列不在缩进位置(这是合乎逻辑的,因为 tempCollection 不知何故没有 625 但更少的值。)
您这里没有处理 neighborAliveCounter == 2
的情况。这就是您得到错误计数的原因。下面的代码块只检查小于 2、等于 3 和大于 3。
//decides if to put cell alive or dead
if (neighborAliveCounter < 2 || neighborAliveCounter > 3)
{
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.Transparent), IsAlive = false });
}
if (neighborAliveCounter == 3)
{
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.LightSeaGreen), IsAlive = true });
}