簡體   English   中英

如何在Xamarin Forms XLabs中使用INetwork?

[英]How can I use INetwork in Xamarin Forms XLabs?

我在使用XLabs.Platform中的INetwork時遇到問題。 我正在獲取System.NullReferenceException。 我不知道,如果問題類似於 ,我需要初始化百寶箱-生活查詢。 但是,由於我是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的文檔已過時。 (顯然)以前運行的“可達性”界面不再起作用。 考慮使用代替。 是一種需要跨平台代碼的方法。 由於作者本人會散布這種方法,因此不會使用它

如果要檢查是否有Internet連接,建議使用連接插件 (Xamarin組件)。 添加Xaamrin組件后,您可以檢查PCL / Shared項目是否可以連接Internet,如下所示

if (CrossConnectivity.Current.IsConnected)
{ // Make API call here 
} 
else 
      DisplayAlert("Connectivity Issue", "Please Connect to a Network", "OK") ;

通過添加以下屬性來填充ioc:

[assembly: Xamarin.Forms.Dependency(typeof(NameOfClassImplementingAnInterface))]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM