XNA Texture2D Dispose() + ObjectDisposedException
I am very new to XNA framework. I am writing a sample application in XNA for windows phone 7.
presently I am facing a problem.
In the sample, i am loading a Texture2D and dispose it in the next line and assign it to null. Again I load the same image to the same member variable. But in the draw I get ObjectDisposedException.
If I remove the dispose call it will not give any exception.
Please help me to solve this.
Sample:
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(开发者_如何学JAVAgameTime);
}
The ContentManager that you are using automatically manages the lifetime of assets. It caches the "Back" texture after the first call and returns the same instance the second time you ask for it. Unfortunately you have asked the Texture to dispose itself so it is no longer in a usable state.
You can use Content.Unload
to remove the texture from memory.
精彩评论