2

我对 XNA 框架很陌生。我正在 XNA 中为 windows phone 7 编写示例应用程序。

目前我面临一个问题。

在示例中,我正在加载一个 Texture2D 并将其放置在下一行并将其分配给 null。我再次将相同的图像加载到相同的成员变量。但在平局中,我得到了 ObjectDisposedException。

如果我删除 dispose 调用,它不会给出任何异常。

请帮我解决这个问题。

样本:

Texture2D texture = null;
 protected override void LoadContent()
 {
      texture = Content.Load<Texture2D>("Back");
      texture .Dispose();
      texture = null;

      texture = Content.Load<Texture2D>("Back");
}


protected override void Draw(GameTime gameTime)
{
      GraphicsDevice.Clear(Color.CornflowerBlue);

      spriteBatch.Begin();
      spriteBatch.Draw(texture , new Vector2(0, 0), Color.White);

      spriteBatch.End();

       base.Draw(gameTime);
}
4

1 回答 1

7

您使用的 ContentManager 会自动管理资产的生命周期。它在第一次调用后缓存“Back”纹理,并在您第二次请求时返回相同的实例。不幸的是,您已要求纹理自行处理,使其不再处于可用状态。

您可以使用Content.Unload从内存中删除纹理。

于 2010-07-22T15:22:31.240 回答