天天看點

WP7-檢查手機網絡 Microsoft.Phone.Net.NetworkInformation Namespace

轉自:http://blog.csdn.net/xuzhongxuan/article/details/6252792

Microsoft.Phone.Net.NetworkInformation Namespace

http://msdn.microsoft.com/en-us/library/ff707715(v=VS.92).aspx

在空間Microsoft.Phone.Net.NetworkInformation下的NetworkInterface 類 和NetworkInterfaceType 類是Windows Phone 7手機網絡資訊相關的類。NetworkInterface提供了目前手機網絡的一些資訊,NetworkInterfaceType是一個手機網絡的類型的枚舉。

下面用一個小例子來使用NetworkInterface和NetworkInterfaceType來檢查手機的網絡狀态。

WP7-檢查手機網絡 Microsoft.Phone.Net.NetworkInformation Namespace

[xhtml]  view plain copy print ?

  1. <Grid   x:Name="LayoutRoot" Background="Transparent">    
  2.         <Grid.RowDefinitions>    
  3.             <RowDefinition   Height="Auto" />    
  4.             <RowDefinition     Height="*" />    
  5.         </Grid.RowDefinitions>    
  6.         <StackPanel  x:Name="TitlePanel"  Grid.Row="0"  Margin="12,17,0,28">    
  7.             <TextBlock  x:Name="PageTitle"  Text="檢查手機網絡"   Margin="9,-7,0,0"   Style="{StaticResource PhoneTextTitle1Style}" mce_Style="{StaticResource PhoneTextTitle1Style}" />    
  8.         </StackPanel>    
  9.         <Grid   x:Name="ContentPanel"   Grid.Row="1"   Margin="12,0,12,0">    
  10.             <TextBox  Name="Message"   Background="Yellow"    Text="Unknown"   VerticalAlignment="Center"    HorizontalAlignment="Center" Margin="131,184,94,378" Width="231" />    
  11.             <Button Content="檢視網絡資訊" Height="72" HorizontalAlignment="Left" Margin="103,6,0,0" Name="button1" VerticalAlignment="Top" Width="244" Click="button1_Click" />    
  12.             <TextBox Height="72" HorizontalAlignment="Left" Margin="131,271,0,0" Name="netname" Text="TextBox" VerticalAlignment="Top" Width="244" />    
  13.             <TextBlock Height="49" HorizontalAlignment="Left" Margin="9,198,0,0" Name="textBlock1" Text="網絡狀态:" VerticalAlignment="Top" Width="121" />    
  14.             <TextBlock HorizontalAlignment="Left" Margin="9,287,0,310" Name="textBlock2" Text="網絡類型:" />    
  15.         </Grid>    
  16.     </Grid>   

[c-sharp:nogutter]  view plain copy print ?

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Net;    
  5. using System.Windows;    
  6. using System.Windows.Controls;    
  7. using System.Windows.Documents;    
  8. using System.Windows.Input;    
  9. using System.Windows.Media;    
  10. using System.Windows.Media.Animation;    
  11. using System.Windows.Shapes;    
  12. using Microsoft.Phone.Controls;    
  13. using System.Net.NetworkInformation;    
  14. using Microsoft.Phone.Net.NetworkInformation;     
  15. namespace checknet    
  16. {    
  17.     public partial class MainPage : PhoneApplicationPage    
  18.     {    
  19.         private bool networkIsAvailable;    
  20.         private NetworkInterfaceType _currentNetworkType; //網絡連接配接的類型    
  21.         public MainPage()    
  22.         {    
  23.             InitializeComponent();    
  24.         }    
  25.         private void button1_Click(object sender, RoutedEventArgs e)    
  26.         {    
  27.             networkIsAvailable = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();//目前網絡是否可用    
  28.             _currentNetworkType = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType;//擷取目前網絡的類型    
  29.             if (networkIsAvailable)    
  30.             {    
  31.                 Message.Text = "聯網狀态";    
  32.                 Message.Background = new SolidColorBrush(Colors.Green);    
  33.             }    
  34.             else   
  35.             {    
  36.                 Message.Text = "斷網狀态";    
  37.                 Message.Background = new SolidColorBrush(Colors.Red);    
  38.             }     
  39.             switch (_currentNetworkType)    
  40.             {    
  41.                 case NetworkInterfaceType.MobileBroadbandCdma:    
  42.                     netname.Text = "Cdma網絡";    
  43.                     break;    
  44.                 case NetworkInterfaceType.MobileBroadbandGsm:    
  45.                     netname.Text = "Csm網絡";    
  46.                     break;    
  47.                 case NetworkInterfaceType.Wireless80211:    
  48.                     netname.Text = "Wireless網絡";    
  49.                     break;    
  50.                 case NetworkInterfaceType.Ethernet:    
  51.                     netname.Text = "Ethernet網絡";    
  52.                     break;    
  53.                 case NetworkInterfaceType.None:    
  54.                     netname.Text = "網絡不可用";    
  55.                     break;    
  56.                 default:    
  57.                     netname.Text = "其他的網絡";    
  58.                     break;    
  59.             }           
  60.         }    
  61.     }    
  62. }   

繼續閱讀