locked
如何获取wp7手机的网络状态 RRS feed

  • 问题

  • 页面上我只有一个TextBlock控件,我想在页面初始化的时候给控件赋值,不知道为什么GetNetName方法取不到值,求大神帮忙看一下。

     // 构造函数
            public MainPage()
            {
                InitializeComponent();
                TBNetStatus.Text = GetNetName();
            }
    
    
            
            public static string GetNetName()
            {
                string reNetName="";
                DeviceNetworkInformation.ResolveHostNameAsync(
                    new DnsEndPoint("www.baidu.com", 80),
                    new NameResolutionCallback(handle =>
                    {
                        NetworkInterfaceInfo info = handle.NetworkInterface;
                        if (info != null)
                        {
                            switch (info.InterfaceType)
                            {
                                case NetworkInterfaceType.Ethernet:
                                    reNetName = "Ethernet";
                                    break;
                                case NetworkInterfaceType.MobileBroadbandCdma:
                                case NetworkInterfaceType.MobileBroadbandGsm:
                                    switch (info.InterfaceSubtype)
                                    {
                                        case NetworkInterfaceSubType.Cellular_3G:
                                        case NetworkInterfaceSubType.Cellular_EVDO:
                                        case NetworkInterfaceSubType.Cellular_EVDV:
                                        case NetworkInterfaceSubType.Cellular_HSPA:
                                            reNetName = "3G";
                                            break;
                                        case NetworkInterfaceSubType.Cellular_GPRS:
                                        case NetworkInterfaceSubType.Cellular_EDGE:
                                        case NetworkInterfaceSubType.Cellular_1XRTT:
                                            reNetName = "2G";
                                            break;
                                        default:
                                            reNetName = "Other";
                                            break;
                                    }
                                    break;
                                case NetworkInterfaceType.Wireless80211:
                                    reNetName = "WiFi";
                                    break;
                                default:
                                    reNetName = "Other";
                                    break;
                            }
                        }
                        else
                            reNetName = "Other";
                    }), null);
                return reNetName;
            }

    2012年3月6日 11:45

答案

全部回复