XNA Texture2D Dispose() +对象处置异常

发布于 2024-09-11 02:37:35 字数 762 浏览 1 评论 0原文

我对 XNA 框架非常陌生。我正在 XNA 中为 Windows Phone 7 编写一个示例应用程序。

目前我遇到了一个问题。

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

如果我删除 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);
}

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(gameTime);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2024-09-18 02:37:35

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文