C# VS2017 IStringable 未引用异常使用 Windows.Data.Json

标签 c# .net json asp.net-mvc uwp

所以我要直接跳进去。

我正在使用 Windows.Data.Json 转换和使用来自 Web 服务的一些 JSON。我知道 Newtonsoft.Json.Net 和其他一些,是的,出于公司和依赖关系的原因,我特别尝试使用 Windows.Data.Json .

该应用程序有 3 个主要部分/组件。

  • UWP Win 10 应用 - 主应用
  • 一个 MVC 网络项目 - 应用程序的网络服务
  • 可移植类库 - 用于两者之间的共享类

我遇到的问题是,当我在 MVC 网络项目或 PCL 中使用 JsonObject 时,出现错误:

The type 'IStringable' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows.Foundation.FoundationContract, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.

这个错误出现在我使用 JsonObject 的任何地方,但是,不在 UWP 应用程序项目中。

例如

//Convert string to json object
var apiJsonObj = JsonObject.Parse(jsonString);

cc.Type = apiJsonObj["TYPE"].ToString();

我在网上搜索过,但找不到任何关于 Windows.Data.JsonIStringable 的有用信息,除此之外: https://learn.microsoft.com/en-us/uwp/api/windows.foundation.istringable

我的项目没有在“添加引用”对话框中列出 Windows.Foundation.FoundationContract,而且我在我正在使用的 UWP 应用中找不到任何对它的引用,也找不到任何其他有用的信息。

UWP 应用程序和 MVC Web 应用程序使用完全相同的代码行来解释 JsonObject。 UWP 应用程序有效。 MVC Web 应用程序出现上述错误。

我想知道是否有其他人一直在成功使用 Windows.Data.Json,并且可能遇到过这个问题,并进行了修复?

应用程序必须尽可能小和独立,因此引用数量少,并且不能使用 Json.Net 等。

最佳答案

This error is showing up everywhere I use the JsonObject, however, NOT in the UWP app project.

Windows.Foundation.FoundationContract 程序集是 platform specific .您不能在可移植图书馆中使用它。它会抛出编译错误,但您可以通过选择以下文件夹中的 winmd 文件添加引用。

C:\Program Files (x86)\Windows Kits\10\References\Windows.Foundation.FoundationContract\2.0.0.0\Windows.Foundation.FoundationContract.winmd

目前,Json.NET 已经支持可移植库,您可以使用它来解析 json。您可以选择 Json.NET,它不会导致复杂的程序集依赖性。

关于C# VS2017 IStringable 未引用异常使用 Windows.Data.Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46238882/

相关文章:

c# - 在递归函数中防止 StackOverFlow

c# - 反编译.Net代码: lambda expressions

Swift 3 中的 JSON 解析错误

c# - 解析 XML 并用元素值填充字符串

c# - 尽管有 SelectedItem 和 ItemsSource 绑定(bind),组合框仍未更新

c# - 在 Windows 服务中的 timer_elapsed 事件处理程序中使用异步等待

javascript - LiveEngage - LivePerson 聊天标签不显示任何内容

php - 当变量存在时ajax下拉框

javascript - 来自 javascript 的 json 记录,espace 空记录

c# - OxyPlot:如何隐藏左侧和顶部轴线