【发布时间】:2017-08-01 11:31:07
【问题描述】:
我正在尝试将本地 json 文件加载到字符串中,以便可以将其解析/反序列化为对象。
但是,尽管所有示例和 Xamarin 支持文档都建议我可以使用 System.IO.File.ReadAllText(...),但由于某种原因,我收到以下错误:
c# error: 'File' type or namespace name not found
我的代码看起来很简单,我无法通过加载文件,看起来应该很简单!
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 上的第一篇文章,希望我做得对..
【问题讨论】:
-
嗨!对不起,我的英语很差,但我也确实在这种事情上挣扎了很多。如果您使用的是 Xamarin.Forms,请确保不要在 PCL 项目中实现特定平台代码。 PCL 的 .Net Framework 包与我们在 xamarin 之外所知道的包有点不同……您需要使用依赖注入来使其正常工作。看这里:developer.xamarin.com/guides/xamarin-forms/…
标签: c# json xamarin xamarin.forms