我在使用来自XLabs.Platform的XLabs.Platform时遇到了问题。我要去接System.NullReferenceException。我不确定这个问题是否类似于需要初始化这的INetwork。但是,因为我对Xamarin非常陌生,所以我不知道如何设置IOC容器。我想以一种完全跨平台的方式来实现这个目标(如果可以的话,我想避免使用特定于平台的代码)。谢谢!!
代码:
using XLabs.Ioc;
using XLabs.Platform;
...
namespace XXX
{
class XXX
{
public static bool isOnline()
{
var networkservice = DependencyService.Get<XLabs.Platform.Services.INetwork>();
var status = networkservice.InternetConnectionStatus();
return (
(status.Equals(XLabs.Platform.Services.NetworkStatus.ReachableViaCarrierDataNetwork))
|| (status.Equals(XLabs.Platform.Services.NetworkStatus.ReachableViaWiFiNetwork))
|| (status.Equals(XLabs.Platform.Services.NetworkStatus.ReachableViaUnknownNetwork)));
}
}
}PS使用XLabs.Platform的文档已过时。以前起作用的Reachability接口(显然)不再工作了。考虑使用这代替。这是一种需要跨平台代码的方法。不会用这个作为作者本人不同意这种方法。
发布于 2016-04-28 15:26:29
如果您想检查是否有任何互联网连接,我建议使用连通插件 (Xamarin组件)。在添加了Xaamrin组件之后,您可以检查来自PCL/Shared项目的internet连接是否是可以从下面的
if (CrossConnectivity.Current.IsConnected)
{ // Make API call here
}
else
DisplayAlert("Connectivity Issue", "Please Connect to a Network", "OK") ;发布于 2016-04-28 15:16:58
您可以通过添加以下属性来填充ioc:
[assembly: Xamarin.Forms.Dependency(typeof(NameOfClassImplementingAnInterface))]https://stackoverflow.com/questions/36918423
复制相似问题