C# 由Foreach引起的巨大红色X Winform/MonoGames

C# 由Foreach引起的巨大红色X Winform/MonoGames,c#,winforms,foreach,monogame,C#,Winforms,Foreach,Monogame,到目前为止,我一直在使用导致巨大红色X的foreach语句。即使我/*注释了除构造函数之外的所有内容。我不明白为什么foreach会在我的应用程序中产生所有问题 public class Map { [XmlElement("Layer")] public List<Layer> Layers; public Vector2 TileDimensions; public void Initialize(ContentManager content)

到目前为止,我一直在使用导致巨大红色X的
foreach
语句。即使我
/*
注释了除构造函数之外的所有内容。我不明白为什么foreach会在我的应用程序中产生所有问题

public class Map
{
    [XmlElement("Layer")]
    public List<Layer> Layers;
    public Vector2 TileDimensions;

    public void Initialize(ContentManager content)
    {
        foreach (Layer l in Layers)
            l.Initialize(content, TileDimensions);
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        foreach (Layer l in Layers) //This is causing the problems
            l.Draw(spriteBatch); 
    }
  }
}
公共类映射
{
[XmlElement(“层”)]
公共列表层;
公共矢量2瓷砖尺寸;
公共无效初始化(ContentManager内容)
{
foreach(层中的层l)
l、 初始化(内容、瓷砖尺寸);
}
公共作废抽签(SpriteBatch SpriteBatch)
{
foreach(Layer l in Layers)//这是导致问题的原因
l、 绘制(spriteBatch);
}
}
}
图层类别是什么

public class Layer
{
  /*  public class TileMap // For some reason it still gets the giant red X even know it commented out.
    {
        [XmlElement("Row")]
        public List<string> Row;
    }

    Vector2 tileDimensions;
    [XmlElement("TileMap")]
    List<List<Vector2>> tileMap;

    TileMap tileLayout = new TileMap();

    public Image image;

   public Layer()
    {
        tileDimensions = new Vector2();
        tileMap = new List<List<Vector2>>();
    }
    */
    public void Initialize(ContentManager content, Vector2 tileDimensions)
    {
       /* foreach (string row in tileLayout.Row)
        {
            string[] split = row.Split(']');
            List<Vector2> tempTileMap = new List<Vector2>();
            foreach (string s in split)
            {
                int value1, value2;
                if (s != String.Empty && !s.Contains('x'))
                {
                    string str = s.Replace("[", String.Empty);
                    value1 = int.Parse(str.Substring(0, str.IndexOf(':')));
                    value2 = int.Parse(str.Substring(str.IndexOf(':') + 1));
                }
                else
                    value1 = value2 = -1;
                tempTileMap.Add(new Vector2(value1, value2));
            }
            tileMap.Add(tempTileMap);
        }
        image.Initialize(content);
        this.tileDimensions = tileDimensions;*/
    }

    public void Draw(SpriteBatch spriteBatch)
    { 
      /*  for (int i = 0; i < tileMap.Count; i++)
        {
            for (int j = 0; j < tileMap[i].Count; j++)
            {
               if (tileMap[i][j] != -Vector2.One)
                {
                    image.position = new Vector2(j* tileDimensions.X, i* tileDimensions.Y);
                    image.SourceRect = new Rectangle((int)(tileMap[i][j].X * tileDimensions.X),
                        (int)(tileMap[i][j].Y * tileDimensions.Y), (int)tileDimensions.X, 
                        (int)tileDimensions.Y);
                    image.Draw(spriteBatch);
                }
            }
         }*/
       }
    }
  }
公共类层
{
/*公共类TileMap//出于某种原因,即使知道它被注释掉了,它仍然会得到巨大的红色X。
{
[XmlElement(“行”)]
公共列表行;
}
矢量2瓷砖尺寸;
[xmlement(“TileMap”)]
列表tileMap;
TileMap tileLayout=新的TileMap();
公众形象;
公共层()
{
tileDimensions=新向量2();
tileMap=新列表();
}
*/
公共void初始化(ContentManager内容,矢量2 tileDimensions)
{
/*foreach(tileLayout.row中的字符串行)
{
string[]split=row.split(']');
List TENTILEMAP=新列表();
foreach(拆分中的字符串s)
{
int值1、值2;
如果(s!=String.Empty&&!s.Contains('x'))
{
string str=s.Replace(“[”,string.Empty);
value1=int.Parse(str.Substring(0,str.IndexOf(':'));
value2=int.Parse(str.Substring(str.IndexOf(':')+1));
}
其他的
value1=value2=-1;
添加(新向量2(value1,value2));
}
tileMap.Add(tileMap);
}
初始化(内容);
this.tileDimensions=tileDimensions*/
}
公共作废抽签(SpriteBatch SpriteBatch)
{ 
/*for(int i=0;i

为什么
foreach
循环使巨大的红色X显示?我需要这个功能使地图显示部分正常工作,这样我就可以编辑地图。所以这真的很重要。我创建了另一篇帖子,这是在我发现
foreach
循环正在破坏它之前。我希望有人能帮我解决这个问题。我已经花了3天的时间试图取消它了解它为什么会崩溃。

Xna/OpenGL在Winforms中很难连接。你确定你的spritebatch使用的是简单的纹理(没有图层之类的东西)吗。如果我是你,我会放弃不推荐使用的Winforms,转而使用WPF。我正在尝试为我的游戏和我的团队创建一个平铺集贴图编辑器,所以我不能放弃。我需要这个。我不明白foreach为什么会破坏它。正如我所说,首先用spritebatch尝试一个简单的纹理,看看问题是否是XNA/Winforms本身的组合。and我认为有一些usercontrols可以在WPF中渲染XNA对象,这可以节省您几天的时间。我无法加载纹理,因为我没有XML文件来显示纹理,这将无法工作,因为巨大的红色X挡住了它。这就是它的外观。有一个X,因为该控件无法渲染您想要的内容,它将我真的不明白为什么你需要一个XML文件来测试.xnb纹理。我刚才看到你使用的是image.Draw(spritebatch)而不是普通的Monogame/Xna spritebatch.Draw。这很奇怪。