简体   繁体   English

为什么导航发生时连接插件网络处理程序不起作用

[英]Why connectivity plugin network handler not working when navigation happens

In case the network connectivity isn't on to check if it is off, so, that I'm using to connectivity plug in. 万一网络连接没有打开以检查它是否关闭,那么我就使用了连接插件。

I'm calling this code in ViewModelLocator class 我在ViewModelLocator类中调用此代码

Private static async void NetworkConnectivityChanged(object sender,Plugin.Connectivity.Abstractions.ConnectivityChangedEventArgs e){}
CrossConnectivity.Current.ConnectivityChanged = NetworkConnectivityChanged;

In my windows app after navigating if network connectivity status changed … Here this event is not fire but if not using navigation, we change the network status and it happens it's working. 导航后,在我的Windows应用程序中是否更改了网络连接状态…在此事件不会触发,但是如果不使用导航,我们会更改网络状态,并且它正在运行。

The workaround is to implement the native network change handler on Winphone or UWP side and stop handling of network change on PCL side for just Winphone and UWP. 解决方法是在Winphone或UWP端实现本机网络更改处理程序 ,并停止仅在Winphone和UWP的PCL端处理网络更改。 You can do this by checking the platform before handling. 您可以通过在处理之前检查平台来做到这一点。

Create a new Network.cs class with the following code(This detects if there is any change in network connection) 使用以下代码创建一个新的Network.cs类(它检测网络连接是否发生任何变化)

 public class InternetConnectionChangedEventArgs : EventArgs

{

    public InternetConnectionChangedEventArgs(bool isConnected)

    {

        this.isConnected = isConnected;

    }



    public bool IsConnected

    {

        get { return this.isConnected; }

    }



    private bool isConnected;

}



public static class Network

{

    public static event EventHandler<InternetConnectionChangedEventArgs>

        InternetConnectionChanged;



    static Network()

    {

        NetworkInformation.NetworkStatusChanged += (s) =>

        {

            if (InternetConnectionChanged != null)

            {

                var arg = new InternetConnectionChangedEventArgs(IsConnected);

                InternetConnectionChanged(null, arg);

            }

        };

    }



    public static bool IsConnected

    {

        get

        {

            var profile = NetworkInformation.GetInternetConnectionProfile();

            var isConnected = (profile != null

                && profile.GetNetworkConnectivityLevel() ==

                NetworkConnectivityLevel.InternetAccess);

            return isConnected;

        }

    }

}

Then in the app.xaml.cs in UWP or WinPhone register the network change handler in OnLaunched event like below 然后在UWP或WinPhone中的app.xaml.cs中,在OnLaunched事件中注册网络更改处理程序,如下所示

Network.InternetConnectionChanged += this.Network_InternetConnectionChanged;

and here is the event handler 这是事件处理程序

 private void Network_InternetConnectionChanged(object sender,InternetConnectionChangedEventArgs e)

    {
      if(e.IsConnected){
         ///code to handle when the internet connectivity is there
      }
      else{
        //code to handle when the internet connectivity is lost
      }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我导航到ASP.Net应用程序时发生了两次导航,为什么会发生这种情况? - Two navigation occurred when I navigate to ASP.Net application, any idea why that happens? 丢失网络连接时ZMQ Pub-Sub程序失败 - ZMQ Pub-Sub Program Failure When Losing Network Connectivity 检查网络连接 - Check for network connectivity 以编程方式断开网络连接 - Programmatically disconnect network connectivity 为什么这个 UnhandledException 事件处理程序不起作用? - Why is this UnhandledException event handler not working? 拔下网线时插座会发生什么变化? - What happens to sockets when I unplug a network cable? ASHX处理程序无法与网络外的客户端正常工作 - ASHX Handler not working correctly with clients outside my network CRM 2013-插件执行(后期操作)什么时候发生? - CRM 2013 - Plugin Execution (Post Operation) What happens and when? 数据库连接:如何检查它,取决于网络连接状态 - DB Connectivity: How to check it, depending on Network connectivity status 从事件处理程序进行更新时,即使触发了Property Changed,MVVM View绑定也不会更新 - MVVM View binding does not update even with Property Changed fired when update happens from an event handler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM