Xamarin Forms iOS中的网络连接更改时如何获得通知 [英] How to get notified when network connection changes in Xamarin Forms iOS

查看:83
本文介绍了Xamarin Forms iOS中的网络连接更改时如何获得通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个要求,我需要显示图像以指示该应用程序是否已连接到网络.我能够使用James Montemagno的Connectivity Plugin实现此功能,但我想使用可达性类.当我实现Reachability类时,永远不会触发OnChange方法.当我打开或关闭wifi时,永远不会调用OnChange.

I have a requirement in my app where I need to display an image to indicate whether the app is connected to network or not.I was able to do it using the Connectivity Plugin by James Montemagno.But I want to implement it using Reachability class.When I implement the Reachability class the OnChange method never fires.When I turn ON or turn OFF the wifi the OnChange is never called.Can somebody guide me on how to achieve this?

 public static event EventHandler ReachabilityChanged;
static void OnChange(NetworkReachabilityFlags flags)
        {
            ReachabilityChanged?.Invoke(null, EventArgs.Empty);
        }

推荐答案

在PCL App()中放置以下代码

Put below code in your PCL App()

CrossConnectivity.Current.ConnectivityChanged += (object sender, Plugin.Connectivity.Abstractions.ConnectivityChangedEventArgs e) =>
{
    bool IsInternetConnected = e.IsConnected;
}

您可以使用Messaging Center传递连接状态,请参考

You can pass connectivity status using Messaging Center refer this

这篇关于Xamarin Forms iOS中的网络连接更改时如何获得通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆