Windows Phone 8 Connection Handler / Internet Availability

后端 未结 3 1939
暖寄归人
暖寄归人 2020-12-10 04:10

My team is working on a team project aplication. At the moment, we need an event handler to check the connection status (if it\'s on/off).

I had big hopes in the <

3条回答
  •  悲&欢浪女
    2020-12-10 05:04

    I haven't tried the System.Net.NetworkInformation namespace on WP8. But the new WP8 Windows.Networking.Connectivity Windows Phone Runtime namespace works just fine.

    Use Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged to know when network conditions change and use Microsoft.Phone.Net.NetworkInformation.NetworkInterface properties or Windows.Networking.Connectivity.NetworkInformation properties to see what's up.

        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            PrintNetworkStatus();
    
            NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
        }
    
        void NetworkInformation_NetworkStatusChanged(object sender)
        {
            PrintNetworkStatus();
        }
    
        private void PrintNetworkStatus()
        {
            Dispatcher.BeginInvoke(() =>
            MessageBox.Show(NetworkInterface.NetworkInterfaceType +
                            Environment.NewLine +
                            NetworkInterface.GetIsNetworkAvailable()));
        }
    

    When I test this code snippet on my WP8 Lumia 920 it works as expected. On startup when my phone is on WiFi only I see the following MessageBox:

    Connected Messagebox

    And once I shutdown my WiFI router and the WiFi connection on the phone is lost I see the following MessageBox:

    Disconnected MessageBox

提交回复
热议问题