MonoGame中的ContentLoadException

我一直在尝试使用Xamarin Studio在MonoGame中加载纹理。 我的代码设置如下:

#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 { ///  /// This is the main type for your game ///  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; } ///  /// 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. ///  protected override void Initialize () { // TODO: Add your initialization logic here base.Initialize (); } ///  /// LoadContent will be called once per game and is the place to load /// all of your content. ///  protected override void LoadContent () { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch (GraphicsDevice); //Content texture = Content.Load("player"); } ///  /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. ///  /// Provides a snapshot of timing values. 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); } ///  /// This is called when the game should draw itself. ///  /// Provides a snapshot of timing values. 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); } } } 

当我调试它时,它给了我错误:

Microsoft.Xna.Framework.Content.ContentLoadException:无法将播放器资源加载为非内容文件! —> Microsoft.Xna.Framework.Content.ContentLoadException:找不到目录。 —> System.IO.DirectoryNotFoundException:找不到路径’C:\ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ bin \ Debug \ Content \ player.xnb’的一部分。 —> System.Exception:

—内部exception堆栈跟踪结束—

在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)

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

在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享)

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

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

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

—内部exception堆栈跟踪结束—

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

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

—内部exception堆栈跟踪结束—

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

在Microsoft.Xna.Framework.Content.ContentManager.Load [T](String assetName)

在TestGame.Game1.LoadContent()中的c:\ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Game1.cs:0

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

在c:\ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Game1.cs中的TestGame.Game1.Initialize():0

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

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

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

在TestGame.Program.Main()中的c:\ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Program.cs:0

那么我做错了什么?

将png文件的'Build action'设置为'Content' ,并将'Copy to output directory''Copy if newer'

您可以通过单击文件并按属性来调出Xamarin Studio中的属性窗口。

您不应该包含文件扩展名。

您需要将文件添加到解决方案的Content目录中,并在属性窗口中将其设置为content / copy(如果较新)。 然后,它将在构建过程中复制到输出目录。

请注意,您可以使用预编译的XNB文件(通常使用XNA游戏工作室创建),也可以使用原始PNG图像文件。 如果您使用后者,则需要在代码中添加文件扩展名。

只是用这种方式

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

代替

 texture = Content.Load("player"); 

将Content文件夹移动到Assets,并为每个资源文件将’Build action’设置为’AndroidAsset’并将’Copy to output directory’设置为’Copy if newer’。

为了让它工作,只需按照我的指示:

与Xamarin的简单2D游戏

我知道它有点长,但它确实相信我,如果有其他问题可以随意问:D问Schreda