MonoGame - 很奇怪"找不到内容文件。"例外

时间:2015-10-10 21:30:34

标签: c# image visual-studio-2013 monogame

是的,我知道你的想法,可能就像"你有错路吗? -_-"我可以向你保证,我有正确的道路。并不是说它一直在运行,但是当异常弹出时,我会选择继续,程序通常会继续,就像什么也没发生一样。即使是图像也有错误的路径"被正确加载和绘制。 即使我认为在实际的Monogame中有一些混乱,我会给你我的代码:

图像类(只有一段代码,类很大)

    public float Alpha;
    public String FontName, Text;
    public String Path;
    [XmlIgnore]
    public Texture2D Texture;
    public Vector2 Scale;
    public Vector2 Position;
    [XmlIgnore]
    public Rectangle SourceRect;

    public bool CenterScale;
    public bool IsActive;

    SpriteFont font;
    Vector2 origin;
    ContentManager content;
    RenderTarget2D renderTarget;

    Dictionary<String, ImageEffect> effectList;

    public Image(String path) {
        Initialize(path);
    }

    public Image () {
        Initialize(String.Empty);
    }

    void Initialize(string path) {
        Text = Path = String.Empty;
        Path = path;
        FontName = "Arial";
        Position = Vector2.Zero;
        Scale = Vector2.One;
        Alpha = 1.0F;
        SourceRect = Rectangle.Empty;
        CenterScale = true;
        effectList = new Dictionary<String, ImageEffect>();
    }

    public void LoadContent () {
        content = new ContentManager(ScreenManager.Instance.Content.ServiceProvider, "Content");

        if (Path != String.Empty)
            Texture = content.Load<Texture2D>(Path); //error here

        font = content.Load<SpriteFont>(FontName);

        Vector2 dimensions = Vector2.Zero;

        if (Texture != null)
            dimensions.X += Texture.Width;
        dimensions.X += font.MeasureString(Text).X;

        if (Texture != null)
            dimensions.Y += Math.Max(Texture.Height, font.MeasureString(Text).Y);
        else
            dimensions.Y += font.MeasureString(Text).Y;

        if (SourceRect == Rectangle.Empty)
            SourceRect = new Rectangle(0, 0, (int)dimensions.X, (int)dimensions.Y);

        if (dimensions.X == 0)
            dimensions.X++;
        if (dimensions.Y == 0)
            dimensions.Y++;

        renderTarget = new RenderTarget2D(ScreenManager.Instance.GraphicsDevice, (int)dimensions.X, (int)dimensions.Y);

        ScreenManager.Instance.GraphicsDevice.SetRenderTarget(renderTarget);
        ScreenManager.Instance.GraphicsDevice.Clear(Color.Transparent);
        ScreenManager.Instance.SpriteBatch.Begin();
        if (Texture != null)
            ScreenManager.Instance.SpriteBatch.Draw(Texture, Vector2.Zero, Color.White);
        ScreenManager.Instance.SpriteBatch.DrawString(font, Text, Vector2.Zero, Color.White);
        ScreenManager.Instance.SpriteBatch.End();

        Texture = renderTarget;

        ScreenManager.Instance.GraphicsDevice.SetRenderTarget(null);
    }

图像创作:

    Background = new Image("Backgrounds/MenuBackground.png");
    Background.LoadContent();       

这是我的内容文件夹:
Tree
我想我已经取消了一些&#34;符号加载&#34;或者像在Visual Studio中那样思考。这会导致异常吗?

2 个答案:

答案 0 :(得分:1)

Monogame有一个新的(取决于你的新定义)向项目添加内容的方式,即通过Monogame Content Pipeline工具。如果您使用Windows,它应该位于此处:

C:\ Program Files(x86)\ MSBuild \ MonoGame \ v3.0 \ Tools

C:\ Program Files \ MSBuild \ MonoGame \ v3.0 \ Tools

使用Pipeline工具打开.mgcb Content Pipeline文件,添加所有纹理并构建它们。 (默认情况下,.mgcb应位于Content文件夹中!)

答案 1 :(得分:0)

好吧,我在这个项目上工作了2天,当我回来时它似乎再次起作用了。我认为计算机重启有帮助。