Monogame - "The content file was not found" 异常,尽管内容文件位于异常指定的位置

Monogame - "The content file was not found" exception despite the content file being the location specified by the exception

我在尝试在 monogame 中加载 .png 文件时遇到了一个不常见的问题。

尽管文件位于相同的确切位置,但我收到一条异常消息:

Unhandled Exception: Microsoft.Xna.Framework.Content.ContentLoadException: The content file was not found. ---> System.IO.FileNotFoundException: Could not find file 'C:\Users\...\RiderProjects\PointAndClickEngine\PointAndClickEngine\Content\EnabledButton
.xnb'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Bo
olean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)
   --- End of inner exception stack trace ---
   at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)
   at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)
   at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName)
   at PointAndClickEngine.Game1.LoadContent() in C:\Users\...\RiderProjects\PointAndClickEngine\PointAndClickEngine\Game.cs:line 74
   at Microsoft.Xna.Framework.Game.Initialize()
   at PointAndClickEngine.Game1.Initialize() in C:\Users\...\RiderProjects\PointAndClickEngine\PointAndClickEngine\Game.cs:line 60
   at Microsoft.Xna.Framework.Game.DoInitialize()
   at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)
   at Microsoft.Xna.Framework.Game.Run()
   at PointAndClickEngine.Program.Main() in C:\Users\...\RiderProjects\PointAndClickEngine\PointAndClickEngine\Program.cs:line 11

这是我的目录的图像:

内容文件夹:

Content.mgcb 的内容:

#----------------------------- Global Properties ----------------------------#

/outputDir:bin
/intermediateDir:obj
/platform:Windows
/config:
/profile:Reach
/compress:False

#-------------------------------- References --------------------------------#


#---------------------------------- Content ---------------------------------#

#begin ButtonDisabled.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:ButtonDisabled.png

#begin ButtonEnabled.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:ButtonEnabled.png

#begin Door1.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door1.png

#begin Door2.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door2.png

#begin Door3.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door3.png

#begin Door4.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door4.png

#begin Door5.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door5.png

#begin Door6.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door6.png

#begin TestBackground.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:TestBackground.png

更奇怪的是TestBackground.png不知为何加载成功了。我完全不知道是什么原因造成的。我试过移动文件以查看是否可以正常工作,但无济于事。上网查了一下,似乎没有其他人有这个问题。 Game.cs:

using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PointAndClickEngine
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //When the mouse reaches the rectangle, the camera pans
        Rectangle leftMoveBoundary;
        Rectangle rightMoveBoundary;

        //Border sprites for testing
        Texture2D leftBoundarySprite;
        Texture2D rightBoundarySprite;

        Background background;
        Element[] elements;

        int width;
        int height;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            //temporry file location
            Content.RootDirectory = @"FILELOCATION";
        }

        protected override void Initialize()
        {
            width = 1600;
            height = 900;

            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;

            //Boundary texture. USED FOR TESTING
            leftMoveBoundary = new Rectangle(0, 0, graphics.PreferredBackBufferWidth / 4,
                graphics.PreferredBackBufferHeight);
            rightMoveBoundary = new Rectangle((int) (graphics.PreferredBackBufferWidth * 0.75), 0,
                graphics.PreferredBackBufferWidth / 4, graphics.PreferredBackBufferHeight);

            leftBoundarySprite = new Texture2D(GraphicsDevice, leftMoveBoundary.Width, leftMoveBoundary.Height);
            leftBoundarySprite.SetData<Color>(
                new Color[leftBoundarySprite.Width * leftBoundarySprite.Height].Select(e => Color.Blue).ToArray());

            rightBoundarySprite = new Texture2D(GraphicsDevice, rightMoveBoundary.Width, rightMoveBoundary.Height);
            rightBoundarySprite.SetData<Color>(
                new Color[rightBoundarySprite.Width * rightBoundarySprite.Height].Select(e => Color.Green).ToArray());

            graphics.ApplyChanges();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Load door textures
            Texture2D[] doorTextures = new Texture2D[6];
            for (int i = 0; i < 6; i++)
            {
                doorTextures[i] = Content.Load<Texture2D>("Door" + i + 1);
            }

            elements = new Element[]
            {
                new Door(doorTextures, Vector2.Zero, 10),
                new Door(doorTextures, Vector2.Zero, 10),
                new DoorButton(Content.Load<Texture2D>("EnabledButton"), Content.Load<Texture2D>("DisabledButton"),
                    elements[0] as Door, new Rectangle(0, (int) (height * 1.25), 158, 128)),
                new DoorButton(Content.Load<Texture2D>("EnabledButton"), Content.Load<Texture2D>("DisabledButton"),
                    elements[1] as Door, new Rectangle((int) (width / 1.1), (int) (height * 1.25), 158, 128)),
            };
            //When I delete every content load above this, the program works and no execption is thrown
            background = new Background(Content.Load<Texture2D>("TestBackground"), elements);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            InputHelper.PreviousMouseState = Mouse.GetState();


            foreach (Element e in elements)
            {
                if (e is Door door)
                {
                    door.Update(gameTime);
                }
                else
                {
                    e.Update();
                }
            }

            base.Update(gameTime);
        }

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

            spriteBatch.Begin();

            spriteBatch.Draw(leftBoundarySprite, leftMoveBoundary.Location.ToVector2(), Color.Blue);
            spriteBatch.Draw(rightBoundarySprite, rightMoveBoundary.Location.ToVector2(), Color.Green);

            spriteBatch.Draw(background.Texture, new Vector2(0, 0));

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

感谢任何帮助!

您遇到的问题是它正在尝试加载一个不存在的文件。

异常表明它正在寻找文件 "EnabledButton.xnb",但您显示文件的文件夹名为 "ButtonEnabled.xnb"。所以名字不一样。

我想 "DisabledButton.xnb" 也会出现同样的情况。

所以这些代码行:

 elements = new Element[]
        {
            new Door(doorTextures, Vector2.Zero, 10),
            new Door(doorTextures, Vector2.Zero, 10),
            new DoorButton(Content.Load<Texture2D>("EnabledButton"), Content.Load<Texture2D>("DisabledButton"),
                elements[0] as Door, new Rectangle(0, (int) (height * 1.25), 158, 128)),
            new DoorButton(Content.Load<Texture2D>("EnabledButton"), Content.Load<Texture2D>("DisabledButton"),
                elements[1] as Door, new Rectangle((int) (width / 1.1), (int) (height * 1.25), 158, 128)),
        };

应该这样读:

 elements = new Element[]
        {
            new Door(doorTextures, Vector2.Zero, 10),
            new Door(doorTextures, Vector2.Zero, 10),
            new DoorButton(Content.Load<Texture2D>("ButtonEnabled"), Content.Load<Texture2D>("ButtonDisabled"),
                elements[0] as Door, new Rectangle(0, (int) (height * 1.25), 158, 128)),
            new DoorButton(Content.Load<Texture2D>("ButtonEnabled"), Content.Load<Texture2D>("ButtonDisabled"),
                elements[1] as Door, new Rectangle((int) (width / 1.1), (int) (height * 1.25), 158, 128)),
        };

请注意我是如何将 EnabledButton 更改为 ButtonEnabled 和将 DisabledButton 更改为 ButtonDisabled.