首先我想说这不是 Content.RootDirectory = "Content"; 问题。我已经尝试过使用和不使用那行代码。我有一个名为 FileFactory 的类,它有三个字典。一种用于纹理,一种用于声音,一种用于精灵字体。我还有其他工厂可以为其他类型的对象加载 XML 文件,并且它们工作得很好。这是加载字体的代码:
public static void LoadFonts(ContentManager content)
{
string[] filePaths = Directory.GetFiles(@"Content\fonts\");
string key;
foreach (string s in filePaths)
{
key = Path.GetFileNameWithoutExtension(s);
Fonts.Add(key, content.Load<SpriteFont>(s));
}
}
它给出的错误是:“加载“Content\fonts\ammoFont.xnb”时出错。找不到文件。” 即使该文件显然在那里,考虑到它必须在目录中才能作为字符串添加到 filePaths 数组中。我什至检查了文件夹,它就在那里。这是完整的错误:
Microsoft.Xna.Framework.Content.ContentLoadException 未处理
消息=加载“Content\fonts\ammoFont.xnb”时出错。文件未找到。
\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Game.cs:Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun) at Microsoft.Xna.Framework.Game.Run( ) 在 C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Program.cs:line 15 InnerException: System.IO.FileNotFoundException Message= 中的 WindowsGame1.Program.Main(String[] args)加载“Content\Content\fonts\ammoFont.xnb.xnb”时出错。文件未找到。Source=Microsoft.Xna.Framework StackTrace: 在 Microsoft.Xna.Framework.TitleContainer.OpenStream(String name) 在 Microsoft.Xna.Framework.Content.ContentManager.OpenStream(StringassetName) InnerException:
答:更改此代码:
Fonts.Add(key, content.Load<SpriteFont>(s));
对此:
Fonts.Add(key, content.Load<SpriteFont>("fonts\\" + key));