Monogame - исключение «Файл содержимого не найден», несмотря на то, что файл содержимого является местоположением, указанным в исключении - PullRequest
0 голосов
/ 31 января 2020

У меня необычная проблема при попытке загрузить файлы .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

Вот изображение моего каталога: image

Папка с содержимым: enter image description here

Содержимое 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);
        }
    }
}

Любая помощь приветствуется!

1 Ответ

2 голосов
/ 31 января 2020

Проблема, с которой вы столкнулись, заключается в том, что она пытается загрузить файл, который не существует.

Исключение говорит о том, что ищет файл "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 .

...