使用“Content"在主文件之外

本文关键字:主文件 quot Content 使用 | 更新日期: 2023-09-27 18:11:12

当我尝试这样做的时候Content.Load<Texture2D>("x");在主文件之外,它抛出一个错误,说它在这个上下文中不存在,即使我使用Microsoft.Xna.Framework.Content;有人知道为什么吗?

使用“Content"在主文件之外

您需要使用内容管理器的实例。在你的主类之外,你必须有一个变量,例如你的内容管理器,我的意思是:

public class OtherClass
{
      ContentManager content;
      public OtherClass(IServiceProvider serviceProvider)
      {
          content = new ContentManager(serviceProvider, "Content");
      }
      public void LoadStuff()
      {
           content.Load<Texture2D>("x");
      }
}
public class Game1
{ 
     public void DoStuff()
     {
         OtherClass other = new OtherClass(Services);
         other.LoadStuff();
     }
}