HttpClient 不可用共享库项目 Xamarin
HttpClient not available Shared Library project Xamarin
我来找你是为了解决一个常见问题,但不幸的是,我找不到解决方案。
我做了一个 xamarin.form 应用程序,它不是 PCL,而是一个共享库项目。我想使用 REST api,为此,我需要使用 HttpClient 实例。
好吧,这就是问题所在。使用 PCL,我只需添加 nuget 包 Microsoft HTTP 客户端库,然后我就可以使用 HttpClient class.
对于共享库项目,我无法将此 nuget 包添加到主项目(我认为这是正常的)。所以我将它添加到 .Droid 和 .iOS 中(哦,顺便说一句,我正在使用 mac 和 Xamarin Studio,如果它可以提供帮助的话)。
当我添加 Nuget 包时,我可以添加这一行:using System.Net.Http;但是 class HttpClient 仍然不可用。我所说的不可用的意思是,我不能使用这个 class(找不到类型或名称空间 httpclient,您是否缺少程序集引用?)
有没有办法在共享项目中使用 HttpClient class,而不是 PCL?如果是,如何?
将 Microsoft HTTP 客户端库安装到您的 Android 和 iOS 项目时一定有错误,因为这些项目类型不受支持。
Could not install package 'Microsoft.Bcl.Build 1.0.14'. You are trying to install this package into a project that targets 'MonoAndroid,Version=v6.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
HttpClient
附带 Xamarin 基础 class 库 https://developer.xamarin.com/api/namespace/System.Net.Http/。
因此您不需要 Microsoft HTTP 客户端库,只需
- 在您的 Android/iOS 项目中引用您的共享项目
- 将库
System.Net.Http
添加到您的 Android/iOS 项目
- 添加
using System.Net.Http
.
在 Xamarin Studio 上:编辑引用 > 包 > 检查 System.Net.Http
在 Visual Studio 上:添加引用 > 程序集 > 框架 > 检查 System.Net.Http
Protip:另外,您可以看看 Paul Betts 的 ModernHttpClient:https://github.com/paulcbetts/ModernHttpClient and Kerry Lothrops blog post about HttpClientHandlers http://kerry.lothrop.de/httpclient-flavors/
我来找你是为了解决一个常见问题,但不幸的是,我找不到解决方案。
我做了一个 xamarin.form 应用程序,它不是 PCL,而是一个共享库项目。我想使用 REST api,为此,我需要使用 HttpClient 实例。 好吧,这就是问题所在。使用 PCL,我只需添加 nuget 包 Microsoft HTTP 客户端库,然后我就可以使用 HttpClient class.
对于共享库项目,我无法将此 nuget 包添加到主项目(我认为这是正常的)。所以我将它添加到 .Droid 和 .iOS 中(哦,顺便说一句,我正在使用 mac 和 Xamarin Studio,如果它可以提供帮助的话)。 当我添加 Nuget 包时,我可以添加这一行:using System.Net.Http;但是 class HttpClient 仍然不可用。我所说的不可用的意思是,我不能使用这个 class(找不到类型或名称空间 httpclient,您是否缺少程序集引用?)
有没有办法在共享项目中使用 HttpClient class,而不是 PCL?如果是,如何?
将 Microsoft HTTP 客户端库安装到您的 Android 和 iOS 项目时一定有错误,因为这些项目类型不受支持。
Could not install package 'Microsoft.Bcl.Build 1.0.14'. You are trying to install this package into a project that targets 'MonoAndroid,Version=v6.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
HttpClient
附带 Xamarin 基础 class 库 https://developer.xamarin.com/api/namespace/System.Net.Http/。
因此您不需要 Microsoft HTTP 客户端库,只需
- 在您的 Android/iOS 项目中引用您的共享项目
- 将库
System.Net.Http
添加到您的 Android/iOS 项目 - 添加
using System.Net.Http
.
在 Xamarin Studio 上:编辑引用 > 包 > 检查 System.Net.Http
在 Visual Studio 上:添加引用 > 程序集 > 框架 > 检查 System.Net.Http
Protip:另外,您可以看看 Paul Betts 的 ModernHttpClient:https://github.com/paulcbetts/ModernHttpClient and Kerry Lothrops blog post about HttpClientHandlers http://kerry.lothrop.de/httpclient-flavors/