如何在 Windows Phone 7 的 XNA 4 中读取 txt 文件? [英] How to read in a txt file in XNA 4 for Windows Phone 7?

查看:17
本文介绍了如何在 Windows Phone 7 的 XNA 4 中读取 txt 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了上一个问题,但是这似乎不适用于 Windows Phone 7 项目的 XNA 4:XNA 框架进口商

I had a look at this previous question however this doesn't seem to work for XNA 4 for a Windows Phone 7 project: XNA Framework Importers

我一直在尝试使用:

string line; 
using (StreamReader sr = new StreamReader("credits.txt"))
{
    while ((line = sr.ReadLine()) != null)
    {
         //reads line by line until eof        
         //do whatever you want with the text
    }
}

`

但这会引发 System.MethodAccessException 尝试访问该方法失败:System.IO.StreamReader..ctor(System.String)"

but this is throwing a System.MethodAccessException "Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)"

我是否需要考虑为此使用隔离存储?

Do I need to look at using IsolatedStorage for this instead?

请注意,我正在尝试加载一个预先准备好的文件,而不是保存正在运行的应用程序的设置.例如一个包含我只会在运行时读取但需要能够在设计时编辑的积分的文本文件.

Please note I am trying to load a pre-prepared file, not save settings for a running application. e.g. a text file containing credits that I will only read in at run time but need to be able to edit during design time.

推荐答案

找到了;我可以在没有 IsolatedStorage 的情况下做到这一点,只需要使用结构如下的 XML 文件:

Found it; I can do this without IsolatedStorage, just need to use an XML file structured as such:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="System.String">
    <credit>
      Firece Game Hunting

      Developer : Sebastian Gray

      Deer (CC) : Martin Pettitt
      http://www.flickr.com/photos/mdpettitt

    </credit>
  </Asset>
</XnaContent>

然后像这样加载 XML 文件:

and then load the XML file like this:

public string LoadFromFile()
{
    using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create("XMLFile1.xml"))
    {
        reader.MoveToContent();
        reader.ReadToFollowing("credit");
        credits = reader.ReadInnerXml();
    }
    return credits;
}

可以将 XML 文件添加到普通项目(而不是内容项目)并将构建操作设置为内容"并将复制到输出目录设置为始终复制".

The XML file can just be added to the the normal project (not the content project) and set the build action to 'Content' and the Copy to Output Directory to 'Copy always'.

这篇关于如何在 Windows Phone 7 的 XNA 4 中读取 txt 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆