ContentLoadException in MonoGame

I am trying to load a texture into MonoGame using Xamarin Studio. My code is configured as follows:

#region Using Statements using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Storage; using Microsoft.Xna.Framework.Input; #endregion namespace TestGame { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; //Game World Texture2D texture; Vector2 position = new Vector2(0,0); public Game1 () { graphics = new GraphicsDeviceManager (this); Content.RootDirectory = "Content"; graphics.IsFullScreen = false; } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize () { // TODO: Add your initialization logic here base.Initialize (); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent () { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch (GraphicsDevice); //Content texture = Content.Load<Texture2D>("player"); } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update (GameTime gameTime) { // For Mobile devices, this logic will close the Game when the Back button is pressed if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { Exit (); } // TODO: Add your update logic here 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) { graphics.GraphicsDevice.Clear (Color.CornflowerBlue); //Draw spriteBatch.Begin (); spriteBatch.Draw (texture, position, Color.White); spriteBatch.End (); base.Draw (gameTime); } } } 

When I debug it, it gives me an error:

Microsoft.Xna.Framework.Content.ContentLoadException: Failed to load game asset as file without content! ---> Microsoft.Xna.Framework.Content.ContentLoadException: The directory was not found. ---> System.IO.DirectoryNotFoundException: could not find part of the path 'C: \ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Bin \ Debug \ Content \ player.xnb. ---> System.Exception:

--- End of internal exception stack trace ---

at at System.IO .__ Error.WinIOError (Int32 errorCode, String maybeFullPath)

at at System.IO.FileStream.Init (String path, FileMode mode, Access to FileAccess, Int32 rights, useRights boolean, FileShare share, Int32 bufferSize, FileOptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)

at in System.IO.FileStream..ctor (String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)

at in System.IO.FileStream..ctor (String path, FileMode mode, FileAccess access, FileShare sharing)

at in System.IO.File.OpenRead (String path)

at Microsoft.Xna.Framework.TitleContainer.OpenStream (string name)

at at Microsoft.Xna.Framework.Content.ContentManager.OpenStream (String assetName)

--- End of internal exception stack trace ---

at at Microsoft.Xna.Framework.Content.ContentManager.OpenStream (String assetName)

at at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset [T] (String assetName, Action`1 recordDisposableObject)

--- End of internal exception stack trace ---

at at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset [T] (String assetName, Action`1 recordDisposableObject)

in Microsoft.Xna.Framework.Content.ContentManager.Load [T] (string assetName)

in TestGame.Game1.LoadContent () in C: \ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Game1.cs: 0

at Microsoft.Xna.Framework.Game.Initialize ()

in TestGame.Game1.Initialize () in C: \ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Game1.cs: 0

at Microsoft.Xna.Framework.Game.DoInitialize ()

at Microsoft.Xna.Framework.Game.Run (GameRunBehavior runBehavior)

at Microsoft.Xna.Framework.Game.Run ()

in TestGame.Program.Main () in C: \ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Program.cs: 0

So what am I doing wrong?

+4
source share
5 answers

Set the 'Build action' the png file to 'Content' and set the 'Copy to output directory' to 'Copy if newer' .

You can open the properties window in Xamarin Studio by pressing ctrl on the file and clicking properties.

You must not include the file extension.

+8
source

You need to add the file to the content directory of your solution and set it to content / copy if it is newer in the properties window. It will then be copied to the output directory during the build process.

Please note that you can use a pre-compiled XNB file (usually created using the XNA game studio), or you can use raw PNG image files. If you use the latter, you need to add the file extension to your code.

+2
source

Just use this method.

 using (var stream = TitleContainer.OpenStream ("Content/charactersheet.png")) { characterSheetTexture = Texture2D.FromStream (this.GraphicsDevice, stream); } 

instead

 texture = Content.Load<Texture2D>("player"); 
0
source

Move the Contents folder to Assets, and for each resource file, set Build Action to AndroidAsset and set Copy to Output Directory to Copy if new.

0
source

To make it work, follow my instructions:

Simple 2D game with Xamarin

I know him a little long, but it works, trust me, in case of additional questions do not hesitate to ask: D Regards Schreda

-4
source

Source: https://habr.com/ru/post/1482777/


All Articles