Xamarin Forms iOS中的网络连接更改时如何获得通知 [英] How to get notified when network connection changes in 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屋!