Xamarin.Forms无法从Resources Returned Null读取文本文件

时间:2016-03-01 12:27:46

标签: c# xamarin xamarin.forms portable-class-library

我正在使用 Xamarin.Forms ,我正在尝试从资源中读取文本文件,在尝试获取文本文件时读取Code Below Returned Null值。

 var assembly = typeof(BookPage).GetTypeInfo().Assembly;
 Stream stream = assembly.GetManifestResourceStream("AboutResources.txt");

//这里的程序集返回null值,我找不到文本资源

我的代码在以下类继承自ContentPage PLC项目

 public class BookPage : ContentPage

enter image description here

2 个答案:

答案 0 :(得分:4)

以下步骤适用于我:

1 - 首先,我将资源文本文件添加到同一个项目

2 - 文件路径必须类似于"您的项目名称。您的自定义文件夹。文件名"。

var fileName = "MySampleProject.XMLData.rawxmldata.xml".

3 - 确保Build Action是EmbeddedResource。

答案 1 :(得分:3)

//First get the list of all resources available for debugging purpose
assembly.GetManifestResourceNames()

这将列出编写代码的程序集中嵌入的所有资源的所有(完全限定名称)。

链接:http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcenames(v=vs.110).aspx

检查它是否具有您期望的"AboutResources.txt"。 检查您是否错误地嵌入了资源文件,它将不会显示在对GetManifestResourceNames()调用返回的列表中。确保你匹配名称的大小写。

相关问题