我在使用来自XLabs.Platform的INetwork时遇到问题。我得到System.NullReferenceException。我不确定问题是否类似于我需要初始化INetwork的this。但是,因为我对Xamarin非常陌生,所以我不知道如何设置IOC容器。我希望以完全跨平台的方式来实现这个目标(如果我能够避免的话,我想避免使用特定于平台的代码)。谢谢!!如何在Xamarin Forms XLabs中使用INetwork?
代码:
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界面不再工作。考虑使用this代替。 This是一种需要跨平台代码的方法。因为作者本人对此方法不屑一顾。
这就是我所做的 – heights1976