【问题标题】:How can I load a text file in Xamarin Studio?如何在 Xamarin Studio 中加载文本文件?
【发布时间】:2016-12-18 19:11:45
【问题描述】:

我在 Xamarin Studio 中创建了一个新的 MonoGame iOS 解决方案。之后,我在我的项目中添加了一个文本文件(File.txt)。 但是当我在 iPhone 模拟器中运行项目时,Xamarin Studio 没有加载文本文件。我在以下行中收到此错误消息:

System.IO.Stream stream = TitleContainer.OpenStream(filename);

System.IO.FileNotFoundException:找不到文件“/Users/Name/Library/Developer/CoreSimulator/Devices/1B232780-3BD1-4AE9-8475-100219150CEF/data/Containers/Bundle/Application/9631F7D3-CF8C-448A -989B-974E984600D7/Loadtextfile.app/File.txt"。

为什么 Xamarin Studio 找不到文件?我在 Xamarin Studio 中将文本文件添加到我的解决方案中,我的解决方案保存在我的桌面上。

如何加载文本文件?

这里是完整的代码:

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;

namespace Loadtextfile
{
public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    private const string filename = "File.txt";
    string[] strs;
    string tmpLine;

    public void LoadFile()
    {
        System.IO.Stream stream = TitleContainer.OpenStream(filename);
        using (var reader = new StreamReader(stream))
        {
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                strs = line.Split(';');
                //...
            }
        }
    }

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {           
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        LoadFile();
    }

    protected override void Update(GameTime gameTime)
    {

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

        base.Draw(gameTime);
    }
}

}

【问题讨论】:

    标签: c# xamarin monogame


    【解决方案1】:

    右键单击文件并将Build Action设置为“内容”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多