我试图将本地json文件加载到字符串中,以便将其解析/反序列化为对象。
然而,由于某些原因,尽管有所有示例和Xamarin支持文档表明我可以使用System.IO.File.ReadAllText(...),但我还是得到了以下错误:
我的代码看起来很简单,我无法通过加载文件,这似乎应该是如此简单!
string jsonInput = System.IO.File.ReadAllText("example.json", Encoding.UTF8);
Quiz temp = JsonConvert.DeserializeObject<Quiz>(jsonInput);我的标题中也包括System.IO。
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel; //ObservableCollection
using Newtonsoft.Json;
using System.IO;
using System.Reflection;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;`我也尝试过以下解决方案:
var assembly = typeof(LoadResourceText).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("example.json");
string jsonInput = "";
using (var reader = new System.IO.StreamReader(stream))
{
jsonInput = reader.ReadToEnd();
}但是,在这种情况下,没有找到'LoadResourceText',我也无法确定应该替换哪种类型。
如有任何帮助或建议,将不胜感激!
我在StackOverflow上的第一篇文章,希望我做得对.
发布于 2017-08-01 14:09:46
如果要将此文件用作本地资源,请确保该文件可用于每个设备,例如,在Android项目中,选择file/properties/advanced/build操作,并将其设置为embedded resource。有时代码很好,只是设备的文件“不存在”
发布于 2017-08-03 11:16:21
^也见上面的答复^
我自己解决了这个问题。
我发现我的答案隐藏在YkshLeo在Xamarin论坛上的一篇论坛文章中:
...make确保资源的构建操作设置为"Embedded Resource"
我真希望Xamarin能让他们的文档更彻底一点。也许这是我的.Net的虚荣心。
https://stackoverflow.com/questions/45436612
复制相似问题