C# 如何读取Xamarin Forms PCL项目上的文本文件?
我需要在我的Xamarin.Forms PCL项目上读取一个文本文件(嵌入式资源)。 在xamarin文档中,建议使用以下代码:C# 如何读取Xamarin Forms PCL项目上的文本文件?,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我需要在我的Xamarin.Forms PCL项目上读取一个文本文件(嵌入式资源)。 在xamarin文档中,建议使用以下代码: var assembly = typeof(LoadResourceText).GetTypeInfo().Assembly; Stream stream = assembly.GetManifestResourceStream("WorkingWithFiles.PCLTextResource.txt"); string text = ""; using (var
var assembly = typeof(LoadResourceText).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("WorkingWithFiles.PCLTextResource.txt");
string text = "";
using (var reader = new System.IO.StreamReader (stream)) {
text = reader.ReadToEnd ();
}
问题是我找不到这个LoadResourceText是什么。我只发现它是我程序集中的一个类型。但我真的不明白这意味着什么
我也找不到一个清晰实用的解释来解释我需要做什么
有什么帮助吗
感谢阅读现有文件,您需要用PCL项目中的类替换
LoadResourceText
。它用于获取包含嵌入文件的程序集。您还需要将WorkingWithFiles
替换为PCL项目的名称空间
您需要使用System.Reflection添加代码>用于编译代码
如果要在运行时创建文件并在以后读取,可以使用以下方法:
public async Task PCLStorageSample()
{
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("answer.txt",
CreationCollisionOption.ReplaceExisting);
await file.WriteAllTextAsync("42");
}
PCL存储不适用于现有的嵌入式资源文件。谢谢。我需要加载现有的嵌入式资源文件。如果我用项目中的任何类更改LoadResourceText,它会抱怨“类型不包含GetTypeInfo()的定义”。我正在查看文档中的示例项目,LoadResourceText也没有实现它。它只是一个普通的ContentPage。有什么想法吗?@Dpedrinha:您在PCL项目中有此代码吗?是的,在portable项目中。@Dpedrinha:使用System.Reflection添加;好了!谢谢!您是如何发现的?