【问题标题】:WP7 Application.getResourceStream retunring null every timeWP7 Application.getResourceasStream 每次都返回 null
【发布时间】:2013-01-10 02:50:49
【问题描述】:

我正在尝试将 .txt 文件加载到 2D 数组中,我可以从独立存储中的临时文件中执行此操作,但我们的关卡构建器写入 .txt 文件并将它们移动到每个独立存储中是不可能的。

这是一款 Windows Phone 游戏。

文本文件构建操作设置为内容。

复制到输出设置为不复制。

该文件位于基本内容文件夹中。

        var ResrouceStream = Application.GetResourceStream(new Uri("grid_map;component/Data.txt", UriKind.Relative));
        if (ResrouceStream != null)
        {
          //do reading here
        }

我在 if 语句后面有一个断点,当我检查 var 时,它仍然为空,后来在using (StreamReader Reader = new StreamReader(ResrouceStream .Stream)) 中出现空错误

因为 ResrouceStream 为空。

我尝试了大多数方法或读取 .txt 文件,但这是最好的方法

【问题讨论】:

  • 为什么要使用这种过时的方法来检索应用程序中的嵌入式资源,而不是使用project resources有人告诉你这就是你的做法吗?只要告诉我那个感动你的人的名字,我会让他付钱。

标签: c# windows-phone-7 windows-phone xna-4.0


【解决方案1】:

如果您想使用Application.GetResourceStream 加载文件,您必须将其构建操作设置为Resource,而不是Content

【讨论】:

  • 好的,所以我将 Build 操作从 Resource 更改为 Content,它给了我错误:Project item 'obj\Windows Phone\Debug\grid_mapContent.g.resources' could not be built. Embedded Resource is not a supported Build Action for items in the Content subproject.
  • 哦,那是因为您不应该在内容项目中放置文本文件。内容项目专为需要转换以供应用程序使用的文件(如纹理)而设计。但是如果你真的想把文件放在那里,请将构建操作设置为Content,将Copy to output directory 设置为Copy always,然后你可以这样加载流:Application.GetResourceStream(new Uri("content/Data.txt", UriKind.Relative)); 请不要告诉任何人我是向你解释的人,我的生命将受到威胁。
猜你喜欢
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 2022-09-27
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多