【问题标题】:Error CS0433: The type 'JsonValue' exists in both 'System.Json, Version=2.0.5.0' and 'Xamarin.Auth' (CS0433)错误 CS0433:“System.Json,Version=2.0.5.0”和“Xamarin.Auth”中都存在“JsonValue”类型 (CS0433)
【发布时间】:2018-04-19 05:37:55
【问题描述】:

我正在尝试使用 Xamarin.Forms 在 Visual Studio for Mac 中创建一个应用程序。在这个应用程序中,我使用 Xamarin.Auth 来存储一些用户详细信息。现在我想使用 JSON 添加与 API 的连接。我添加了 System.Json 并添加了我的代码。但问题是我得到了错误:

错误 CS0433:“JsonValue”类型存在于“System.Json”中, 版本=2.0.5.0' 和 'Xamarin.Auth' (CS0433)。

我删除了 Xamarin.Auth 并将其添加到项目中。我在关闭 Visual Studio 时删除了 OBJ 和 BIN 文件夹,启动 VS,清理解决方案,重建解决方案并再次尝试,但仍然是同样的错误。我似乎无法找出问题所在。

我不太确定它是否会有所帮助,但这是发生错误的一个 sn-p 代码,我知道该函数目前没有返回任何内容,但我只是想弄清楚该怎么做JSON/API 调用并获取代码以无错误地编译:

public class DBhandler
{
    public async Task<List<Object>> GetItemsFromApiASync(Type callingClass,    string apiDir, string apiFile)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(ApiAddress + apiDir + apiFile));
        request.ContentType = "application/json";
        request.Method = "Get";

        using (WebResponse response = await request.GetResponseAsync())
        {
            using (Stream stream = response.GetResponseStream())
            {
                System.Json.JsonValue jsonDoc = await Task.Run(() => System.Json.JsonObject.Load(stream));
                object[] parametersArray = new object[] { jsonDoc };
                MethodInfo methodInfo = callingClass.GetMethod("ConvertApiResultToObject");
                methodInfo.Invoke(methodInfo, parametersArray);
            }
        }

        return null;
    }

【问题讨论】:

  • 完全限定你的类(即包括命名空间)的使用,以便编译器知道你打算使用哪一个。
  • 尝试在你的命名空间之外设置 using JsonValue = System.Json.JsonValue 并从方法中删除合格的使用
  • 使用 /reference 编译器选项的别名特性
  • @Tcraft 我已将别名添加到 Visual Studio 中的引用中。然后使用外部别名给了我the extern alias was not specified in a reference option,我似乎无法解决。 @DiegoRafaelSouza 我试过了,它给了我同样的错误。 @肯尼斯K。我不确定你是什么意思。我已经完全指定了类(System.Json.JsonValue)。我的类有一个命名空间,但在这段代码 sn-p 中被遗漏了。一个额外有趣的事实:在一堂课中,我使用了using SystemJsonAlias = System.Json,它可以编译。在其他类中,我使用相同,但我再次收到 exists in both 错误。

标签: c# json xamarin.forms xamarin.auth system.json


【解决方案1】:

在创建引用 Xamarin.Auth 和一些其他 NuGet 包和 Facebook Xamarin 组件的 Xamarin Android 项目时,我遇到了同样的问题。我能够简单地从项目的引用中删除 System.Json,然后我的项目就能够编译和运行。

【讨论】:

  • 这不是我的选择。我需要使用 System.Json。我最终得到了它的大量摆弄和运气,并使用using SystemJsonAlias = System.Json 不要问我是什么原因我现在无法复制它(太久以前,不太确定我做了什么)但它有效,所以我离开它。
【解决方案2】:

突然之间,我确实记得如何解决这个问题:cmets 帮助我解决了这个问题(感谢@Tcraft,@DiegoRafaelSouza)。我在不同平台上的引用中添加了System.Json(我使用共享项目,等等project.IOSproject.Android)。右键单击properties 并勾选Local Copy。然后我添加了一个别名SystemJsonAlias。并在我所有需要的 .cs 文件中使用了using SystemJsonAlias = System.Json

也许这对其他人有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    相关资源
    最近更新 更多