The networkinterface class and networkinterfacetype class under Microsoft. Phone. net. networkinformation are related to the network information of Windows Phone 7. Networkinterface provides some information about the current mobile phone network. networkinterfacetype is an enumeration of the type of mobile phone network.
The following example uses networkinterface and networkinterfacetype to check the network status of the mobile phone.
<Grid X: Name = "layoutroot" background = "Transparent"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <stackpanel X: Name = "titlepanel" grid. row = "0" margin = ","> <textblock X: Name = "pagetitle" text = "Check mobile network" margin = "9, 0 "style =" {staticresource phonetexttitle1style} "/> </stackpanel> <grid X: Name =" contentpanel "grid. row = "1" margin = "12, 0, 131,184 "> <textbox name =" message "background =" yellow "text =" unknown "verticalignment =" center "horizontalalignment =" center "margin =, 94,378 "width =" 231 "/> <button content =" view Network Information "Height =" 72 "horizontalalignment =" Left "margin =, 244 "name =" button1 "verticalignment =" TOP "width =" 131,271 "Click =" button#click "/> <textbox Height =" 72 "horizontalalignment =" Left "margin = ", 244 "name =" netname "text =" textbox "verticalalignment =" TOP "width =" 9,198 "/> <textblock Height =" 49 "horizontalalignment =" Left "margin =", 121 "name =" textblock1 "text =" network status: "verticalalignment =" TOP "width =" 9,287 "/> <textblock horizontalalignment =" Left "margin =, 0,310 "name =" textblock2 "text =" network type: "/> </GRID>
Using system; using system. collections. generic; using system. LINQ; using system. net; using system. windows; using system. windows. controls; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. animation; using system. windows. shapes; using Microsoft. phone. controls; using system. net. networkinformation; using Microsoft. phone. net. networkinformation; namespace checknet {public partial class mainpage: phoneapplicationpage {private bool networkisavailable; private networkinterfacetype _ currentnetworktype; // public mainpage () {initializecomponent ();} private void button#click (Object sender, routedeventargs e) {networkisavailable = Microsoft. phone. net. networkinformation. networkinterface. getisnetworkavailable (); // whether the current network is available _ currentnetworktype = Microsoft. phone. net. networkinformation. networkinterface. networkinterfacetype; // obtain the current network type if (networkisavailable) {message. TEXT = "Networking status"; message. background = new solidcolorbrush (colors. green);} else {message. TEXT = "disconnected"; message. background = new solidcolorbrush (colors. red);} switch (_ currentnetworktype) {Case networkinterfacetype. mobilebroadbandcdma: netname. TEXT = "CDMA network"; break; Case networkinterfacetype. mobilebroadbandgsm: netname. TEXT = "csm network"; break; Case networkinterfacetype. wireless80211: netname. TEXT = "wireless network"; break; Case networkinterfacetype. ethernet: netname. TEXT = "Ethernet network"; break; Case networkinterfacetype. none: netname. TEXT = "Network unavailable"; break; default: netname. TEXT = "other networks"; break ;}}}}