Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#For循环不向可观察集合添加项_C#_Wpf_For Loop_Observablecollection - Fatal编程技术网

C#For循环不向可观察集合添加项

C#For循环不向可观察集合添加项,c#,wpf,for-loop,observablecollection,C#,Wpf,For Loop,Observablecollection,因此,我在使用for循环向可观察集合中添加值时遇到了一个问题。我正在尝试向集合中添加625个值,但它跳过了一些值,我不知道为什么,或者我看不见这个问题 正如在代码中看到的,我有两个集合,一个我已经在构造函数中预先填充了值(这没有问题),另一个我正在尝试从底层填充新值。我有一个for循环,循环625次(同样有效),但不知为什么有些值被跳过了,我不知道为什么 也可以说,我有一个方法,在CellCollection中单击某些值时会发生更改,但这发生在for循环之前,实际上与之无关,但可能值得一提 使用

因此,我在使用for循环向可观察集合中添加值时遇到了一个问题。我正在尝试向集合中添加625个值,但它跳过了一些值,我不知道为什么,或者我看不见这个问题

正如在代码中看到的,我有两个集合,一个我已经在构造函数中预先填充了值(这没有问题),另一个我正在尝试从底层填充新值。我有一个for循环,循环625次(同样有效),但不知为什么有些值被跳过了,我不知道为什么

也可以说,我有一个方法,在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 });
                    }
                }
            }
void neightarcalc()
{
//计算活动邻居并杀死或设置单元格活动
for(int i=0;i1)
{
CellModel topCell=CellCollection.Single(cell=>cell.Column==CellCollection[i]。Column&&cell.Row==CellCollection[i]。行-1);
如果(topCell.IsAlive==true)
{
NeightraliveCounter=NeightraliveCounter+1;
}
}
//检查底部
if(单元格集合[i]。行<25)
{
CellModel bottomCell=CellCollection.Single(cell=>cell.Column==CellCollection[i]。Column&&cell.Row==CellCollection[i]。Row+1);
if(bottomCell.IsAlive==true)
{
NeightraliveCounter=NeightraliveCounter+1;
}
}
//左勾
if(CellCollection[i]。列>1)
{
CellModel leftCell=CellCollection.Single(cell=>cell.Column==CellCollection[i]。Column-1&&cell.Row==CellCollection[i]。Row);
if(leftCell.IsAlive==true)
{
NeightraliveCounter=NeightraliveCounter+1;
}
}
//核对正确
if(CellCollection[i].列<25)
{
CellModel rightCell=CellCollection.Single(cell=>cell.Column==CellCollection[i]。Column+1&&cell.Row==CellCollection[i]。Row);
if(rightCell.IsAlive==true)
{
NeightraliveCounter=NeightraliveCounter+1;
}
}
//检查左上角
if(CellCollection[i]。列>1&&CellCollection[i]。行>1)
{
CellModel topLeftCell=CellCollection.Single(cell=>cell.Column==CellCollection[i]。Column-1&&cell.Row==CellCollection[i]。Row-1);
if(topLeftCell.IsAlive==true)
{
NeightraliveCounter=NeightraliveCounter+1;
}
}
//检查右上角的单元格
if(CellCollection[i]。行>1&&CellCollection[i]。列<25)
{
CellModel topRightCell=CellCollection.Single(cell=>cell.Column==CellCollection[i]。Column+1&&cell.Row==CellCollection[i]。Row-1);
if(topRightCell.IsAlive==true)
{
NeightraliveCounter=NeightraliveCounter+1;
}
}
//检查左下角单元格
if(CellCollection[i]。行<25&&CellCollection[i]。列>1)
{
CellModel bottomLeftCell=CellCollection.Single(cell=>cell.Column==CellCollection[i]。Column-1&&cell.Row==CellCollection[i]。Row+1);
if(bottomLeftCell.IsAlive==true)
{
NeightraliveCounter=NeightraliveCounter+1;
}
}
//检查右下角单元格
if(CellCollection[i]。行<25&&CellCollection[i]。列<25)
{
CellModel bottomRightCell=CellCollection.Single(cell=>cell.Column==CellCollection[i]。Column+1&&cell.Row==CellCollection[i]。Row+1);
if(bottomRightCell.IsAlive==true)
{
NeightraliveCounter=NeightraliveCounter+1;
}
}
//决定是让细胞存活还是死亡
if(邻域计数<2 | |邻域计数>3)
{
tempCollection.Add(new-CellModel(){Row=CellCollection[i]。Row,Column=CellCollection[i]。Column,CellBackgroundColor=new-solidcolorbush(Colors.Transparent),IsAlive=false});
}
if(neightraliveCounter==3)
{                        
tempCollection.Add(new-CellModel(){Row=CellCollection[i]。Row,Column=CellCollection[i]。Column,CellBackgroundColor=new-solidcolorbush(Colors.lightsegreen),IsAlive=true});
}
}
}
我不确定为什么它们中的值数量都不相同,并且某些行/列不在缩进点(从tempCollection开始,什么是合乎逻辑的
//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 });
                    }