0
votes

I am trying to load a sprite image file (xnb) and it just cannot find the file. I have tried setting a custom root directory, I have tried just putting the file in the content and it still cant find it. This is my code.

using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.Linq;

namespace screen_switch
{
   
    public class Game1 : Microsoft.Xna.Framework.Game
    {

        // graphics
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;


        //page
        public PageManager PageMgr = new PageManager();
        public PageGame pageGame = new PageGame();
        public PageGame2 pageGame2 = new PageGame2();
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            //init pages
            PageMgr.Add(pageGame, this);
            PageMgr.Add(pageGame2, this);


            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            this.IsMouseVisible = true;
            spriteBatch = new SpriteBatch(GraphicsDevice);
            


            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            PageMgr.Update(gameTime, this);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            KeyboardState kS = Microsoft.Xna.Framework.Input.Keyboard.GetState();
            if (kS.IsKeyDown(Keys.Up))
            {
                PageMgr.Set(pageGame2, this);
            }
            if (kS.IsKeyDown(Keys.Down))
            {
                PageMgr.Set(pageGame, this);
            }
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            PageMgr.Draw(this);

            spriteBatch.Begin();
            Texture2D squareTexture;
            squareTexture = Content.Load<Texture2D>("square");
            spriteBatch.Draw(squareTexture,new  Vector2(100,100), Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

I really don't understand. The error message is:

Microsoft.Xna.Framework.Content.ContentLoadException
  HResult=0x80131500
  Message=Error loading "square". File not found.
  Source=Microsoft.Xna.Framework
  StackTrace:
   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 screen_switch.Game1.Draw(GameTime gameTime) in C:\Users\Bigweld\source\repos\screen switch\screen switch\screen_switch\Game1.cs:line 88
   at Microsoft.Xna.Framework.Game.DrawFrame()
   at Microsoft.Xna.Framework.Game.Tick()
   at Microsoft.Xna.Framework.Game.HostIdle(Object sender, EventArgs e)
   at Microsoft.Xna.Framework.GameHost.OnIdle()
   at Microsoft.Xna.Framework.WindowsGameHost.RunOneFrame()
   at Microsoft.Xna.Framework.WindowsGameHost.ApplicationIdle(Object sender, EventArgs e)
   at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Microsoft.Xna.Framework.WindowsGameHost.Run()
   at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
   at Microsoft.Xna.Framework.Game.Run()
   at screen_switch.Program.Main(String[] args) in C:\Users\Bigweld\source\repos\screen switch\screen switch\screen_switch\Program.cs:line 15

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
FileNotFoundException: Error loading "Content\square.xnb". File not found.

The end goal is to load my square sprite, and paint it SOMEWHERE on the screen. I don't even care where I just need the ability to know I can place a sprite in this damn framework. Please help, thanks!

1
Why are you using XNA at all? XNA reached end of life nearly a decade ago.gunr2171
I see no shame in using XNA as long as it's one's preference, though I'd recommend using Monogame, as that has the same framework as XNA, but remains up to date.Steven

1 Answers

0
votes

The squareTexture = Content.Load<Texture2D>("square"); should be defined in the LoadContent() method.

And make sure your sprite is found in the contents folder in your Solution Explorer. And that the naming/directory is correct.