C# DrawableGameComponent作为库,如何处理内容?

C# DrawableGameComponent作为库,如何处理内容?,c#,windows-phone-7,xna,C#,Windows Phone 7,Xna,我正在尝试为我的游戏创建一个模块,目的是能够在我的所有游戏中使用它 因此,我的lib项目有自己的内容文件夹,而且一切都很好,但它与宿主项目的内容文件夹崩溃了 这是我的组件(它是我引用到主机项目(手机应用程序)链接的自己的项目): 名称空间组件 { 公共类FPS:DrawableGameComponent { 私人SpriteBatch SpriteBatch; 精神正常; 纹理2D头部背景; 公共FPS(游戏) :基地(游戏) { spriteBatch=新spriteBatch(Game.Gr

我正在尝试为我的游戏创建一个模块,目的是能够在我的所有游戏中使用它

因此,我的lib项目有自己的内容文件夹,而且一切都很好,但它与宿主项目的内容文件夹崩溃了

这是我的组件(它是我引用到主机项目(手机应用程序)链接的自己的项目):

名称空间组件
{
公共类FPS:DrawableGameComponent
{
私人SpriteBatch SpriteBatch;
精神正常;
纹理2D头部背景;
公共FPS(游戏)
:基地(游戏)
{
spriteBatch=新spriteBatch(Game.GraphicsDevice);
字符串tempRootDirectory=Game.Content.RootDirectory;
Game.Content.RootDirectory=“FPSComponentContent”;
headerBackground=Game.Content.Load(“标题背景”);
正常=游戏.内容.加载(“正常”);

//Game.Content.RootDirectory=tempRootDirectory;我认为您必须阅读这篇文章

关键是,当您在VisualStudio上点击F5时,它将编译您的项目和资产

通过使用
Game.Content.Load
XNA将尝试找到一个.xnb

当VisualStudio编译您的项目时,他正在将您的.xnb创建到一个特定的文件夹中

您并没有将.png或.jpg文件直接加载到游戏:)

您必须首先将新资产编译成.xnb文件(类似于序列化过程)


这里有一个类似的问题

我已经通过将内容文件夹作为库的一部分成功地解决了这个问题,我已经将预编译的资产复制到了库中。这种方法的缺点是,您必须将资产从内容文件夹复制到构建位置(当引用所述库时).如果你愿意,我会把它贴出来,不过我想看看更好的方法。。。
namespace FPSComponent
{
    public class FPS : DrawableGameComponent
    {
        private SpriteBatch spriteBatch;

        SpriteFont normal;
        Texture2D headerBackground;

        public FPS(Game game)
            : base(game)
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            string tempRootDirectory = Game.Content.RootDirectory;
            Game.Content.RootDirectory = "FPSComponentContent";

            headerBackground = Game.Content.Load<Texture2D>("header-background");

            normal = Game.Content.Load<SpriteFont>("normal");

            //Game.Content.RootDirectory = tempRootDirectory; <-- does not work
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here

            base.Update(gameTime);
        }

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

            spriteBatch.Begin();

            spriteBatch.Draw(headerBackground, Vector2.Zero, Color.White);

            spriteBatch.DrawString(normal, "FPS COMPONENT", new Vector2(20, 20), Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}